0

This is kind of difficult to communicate but I'll try without pasting all my code. I have Members who have one Mailbox which has many Receipts. In the header layout I have a nav that calls

<%= link_to "Message Center", member_mailbox_path(current_user.member_id) %>

It works on most pages like trails/# , the resource pages for various models But on other pages, seems like custom route pages, I get this error

No route matches {:action=>"show", :controller=>"mailbox", :member_id=>16}

Running rake routes shows this:

member_mailbox GET    /members/:member_id/mailbox/:id(.:format)      mailbox#show

Routes are confusing to me, here are my routes for this problem (show message isn't tested yet) ...

resources :members do
 resources :mailbox do
  resources :receipts do
    member do
      get :show_message
    end
  end
 end
end

The routes for the pages that are showing the error are similar to this one

match '/my_plays', :to => "trails#my_plays"
match '/my_creations', :to => "trails#my_creations"

So not sure if my routes are right. I wonder if resources :mailbox is correct since I don't have a bunch of resources for that, it's a has_one .... THX

----EDIT--- after changing route per advice:

member_mailbox POST   /members/:member_id/mailbox(.:format)                            mailboxes#create
new_member_mailbox GET    /members/:member_id/mailbox/new(.:format)                       mailboxes#new
edit_member_mailbox GET    /members/:member_id/mailbox/edit(.:format)                      mailboxes#edit
                    GET    /members/:member_id/mailbox(.:format)                           mailboxes#show
                    PUT    /members/:member_id/mailbox(.:format)                           mailboxes#update
                    DELETE /members/:member_id/mailbox(.:format)                           mailboxes#destroy
HappaGirl
  • 117
  • 1
  • 10

1 Answers1

0

You may want to define a mailbox as a singular resource in your routes. Otherwise, Rails will expect you to pass in both the user id and the mailbox id for member_mailbox_path to route to mailbox#show. I believe this is why you're getting a routing error. Since each user has one mailbox, there's no need to make this extra lookup part of the route. So instead of resources :mailbox, you can do resource :mailbox:

resources :members do
  resource :mailbox do
    resources :receipts do
      member do
        get :show_message
      end
     end
  end
end

I believe this would generate the following routes:

                 member_mailbox POST   /members/:member_id/mailbox(.:format)                           mailboxes#create
             new_member_mailbox GET    /members/:member_id/mailbox/new(.:format)                       mailboxes#new
            edit_member_mailbox GET    /members/:member_id/mailbox/edit(.:format)                      mailboxes#edit
                                GET    /members/:member_id/mailbox(.:format)                           mailboxes#show
                                PUT    /members/:member_id/mailbox(.:format)                           mailboxes#update
                                DELETE /members/:member_id/mailbox(.:format)                           mailboxes#destroy

Notice that the lack of path names next to GET, PUT, and DELETE doesn't mean they don't exist; they're just repeats of the POST path, but each responds to different HTTP methods.

To render mailboxes#show, you'll need to add a MailboxesController with a show route, which might do a look up for the member:

class MailboxesController < ApplicationController

  def show
    @member = Member.find(params[:member_id])
    # other mailbox code...
  end

end 

And you'll also create a template at app/views/mailboxes/show.html.erb to render the mailbox show page.

Also, I would recommend against deeply nesting your routes, as in third level :receipts.

rossta
  • 11,394
  • 1
  • 43
  • 47
  • awsome rossta...worked like a charm...I'm not thrilled about the nesting. – HappaGirl Oct 09 '12 at 21:56
  • eek, I spoke too soon. it takes away my error, but I don't have a route to the show action anymore...weird what routes I do have see EDIT – HappaGirl Oct 09 '12 at 22:04
  • I'm not sure what you mean; this is a show route to mailboxes#show scoped by member: `GET /members/:member_id/mailbox(.:format)`. You can call this in a view with `member_mailbox_path(@member)` or `member_mailbox_path(member_id)` – rossta Oct 09 '12 at 22:23
  • I don't get it either. with your change I now have `uninitialized constant MailboxesController` when I visit `member_mailbox_path(member_id)` ... notice in the route that it doesn't have the path before the GET – HappaGirl Oct 11 '12 at 01:45
  • You may be getting caught up on `rake routes`. Your last comment tells me that the path `member_mailbox_path(member_id)` is working and that you haven't added the `MailboxesController` to your app. Added an edit above. – rossta Oct 11 '12 at 02:00
  • Thanks for your help. While I had all the files and methods you describe, I noticed you have "mailboxes"...my controller and view folder were "mailbox" so I added "es" onto everything and it works!! Cheers – HappaGirl Oct 11 '12 at 04:02