views:

52

answers:

2

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.

A: 

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.

uzzz
I'm not sure this is better. When a user is looking at *their* entries, I don't want the user ID to be part of the URL (it's unnecessary and invites tampering). That is, I'd much rather have /user/entries than /user/1/entries.
jerhinesmith
I was wrong: you can nest resources from resource. But if you do it you don't have to write both objects in your form_for (user and entry).
uzzz
A: 

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).

jerhinesmith