Partial form with nested routes
I have two models -- User
and Entry
-- that are related开发者_运维百科 through a has_many
relationship (a User has many Entries). I'm using RESTful routing, and have the following in my routes.rb file:
map.resource :user, :controller => "users" do |user|
user.resources :entries
end
This seems to work, but in my partial _form file, when I do this:
form_for [@current_user, @entry] do |f|
# Form stuff
end
It generates a URL like this:
/user/entries.%23%3Cuser:0xb6a6aea8%3E
instead of
/user/entries
Am I missing something?
I should note that the correct classes are applied to the form when doing creation vs. editing, so it does seem to be correctly interpreting what I'm trying to do -- it's just that I can't submit the form to an invalid url.
It seems that you can't nest resources from resource (singular). Try to make
map.resources :users do |user|
user.resources :entries
end
moreover, that it's better to use resources for users.
I figured it out. Instead of doing:
form_for [@current_user, @entry] do |f|
# Form stuff
end
I needed to be doing:
form_for [:user, @entry] do |f|
# Form stuff
end
That is, instead of using the @current_user
object itself, I just had to tell the form_for
what my @entry
object was scoped to (in this case, the user
class).
精彩评论