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
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_pathto route tomailbox#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 ofresources :mailbox, you can doresource :mailbox:I believe this would generate the following routes:
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 aMailboxesControllerwith a show route, which might do a look up for the member:And you’ll also create a template at
app/views/mailboxes/show.html.erbto render the mailbox show page.Also, I would recommend against deeply nesting your routes, as in third level :receipts.