Rails can't match the existed route
First I rake routes to check all the routes and make sure it exists in my app.
In my route.rb
resources :user do
resource :account
resource :addresses
end
And now everything is fine so far. I got some path helper method. Such as
user_addresses_path
this helper method works fine in everywhere(I mean it work in every view template)except one place. It can't work under my user's view template. I'll show you blow.
#it works here.
#this file is under app/view/address
<%= user_addresses_path(@user) %>
#it doesn't work here.
#this file is under app/view/user
<%= user_addresses_path(@user) %>
Just for convenient, I don't paste all the codes in here.
But if you understand what I mean, you know, just tell me why is this happening.
Add comment if you开发者_开发技巧 want more detail.
I believe the issue is the way you've got the address route defined as a nested route for the user. Specifically, in the rails documentation it states:
Passing a record (like an Active Record or Active Resource) instead of a Hash as the options parameter will trigger the named route for that record. The lookup will happen on the name of the class. So passing a Workshop object will attempt to use the workshop_path route. If you have a nested route, such as admin_workshop_path you’ll have to call that explicitly (it’s impossible for url_for to guess that route).
In other words, because the route being referred to is defined as a nested route, rails can't guess the route. Since addresses are nested inside the user, it can guess the user when on a specific address, but not the address when up at the user level.
Also, is it possible that what you have is a 'one-to-many' user-to-address relationship? If that's the case, then your resource
might need to be resources
(plural) in your routes file.
resources :user do
resource :addresses
end
gives you:
user_addresses POST /user/:user_id/addresses(.:format) {:action=>"create", :controller=>"addresses"}
new_user_addresses GET /user/:user_id/addresses/new(.:format) {:action=>"new", :controller=>"addresses"}
edit_user_addresses GET /user/:user_id/addresses/edit(.:format) {:action=>"edit", :controller=>"addresses"}
GET /user/:user_id/addresses(.:format) {:action=>"show", :controller=>"addresses"}
PUT /user/:user_id/addresses(.:format) {:action=>"update", :controller=>"addresses"}
DELETE /user/:user_id/addresses(.:format) {:action=>"destroy", :controller=>"addresses"}
but,
resources :user do
resources :addresses
end
gives you:
POST /user/:user_id/addresses(.:format) {:action=>"create", :controller=>"addresses"}
new_user_address GET /user/:user_id/addresses/new(.:format) {:action=>"new", :controller=>"addresses"}
edit_user_address GET /user/:user_id/addresses/:id/edit(.:format) {:action=>"edit", :controller=>"addresses"}
user_address GET /user/:user_id/addresses/:id(.:format) {:action=>"show", :controller=>"addresses"}
PUT /user/:user_id/addresses/:id(.:format) {:action=>"update", :controller=>"addresses"}
DELETE /user/:user_id/addresses/:id(.:format) {:action=>"destroy", :controller=>"addresses"}
Notice that the second option (plural) gives you routes to be able to address multiple addresses for each user, whereas the singular route only gives you a single address.
精彩评论