RESTful routes: How to code the "create" form correctly

Hi there,

I’ve been trying to fix this for the whole afternoon, but don’t get it
to work:

I have:

-> Nested RESTful resources/routes like this:

map.resources :users, :member => { :enable => :put, :view => :get } do
|users|
users.resource :account
users.resources :roles
users.resources :messages
end

-> A link on a page (should bring up a form for a new message
“new.html.erb”):

<%= link_to ‘Send a message’, new_user_message_path(@user, :target_id
=> @member.id) %>

-> The “MessagesController” like this:

def new
@target = User.find(params[:target_id])
@message = Message.new
end

-> And the message form “new.html.erb” like this:

<% form_for @message do |f| %>


Subject:
<%= f.text_field :subject, :focus => true %>


[…etc…]
<% end %>

…but I get the error:

NoMethodError in Messages#new
Showing messages/new.html.erb where line #4 raised:
undefined method `messages_path’ for #ActionView::Base:0x554b884

Line 4 is: <% form_for @message do |f| %>

As far as I know, with RESTful routes, it’s sufficient to only have the
@message” in the form header. Rails 2.0 should automatically understand
that “@message” is a new message and therefor redirect me to the “new”
form, right?

Thank you very much for any hint which could help me get out of the
mess!
Tom

NoMethodError in Messages#new
Showing messages/new.html.erb where line #4 raised:
undefined method `messages_path’ for #ActionView::Base:0x554b884

Line 4 is: <% form_for @message do |f| %>

Correct me if I’m wrong, but as far as I know the form_for helper is not
able to deal with nested resources when used in the super compact
fashion above. You’re right, Rails knows that it is dealing with a new
object; however it tries to use message_path instead of
user_messages_path(@user) to create the url. This format should work:

<% form_for :messages, @message,
:url => user_messages_path(@user) do |f| %>

Cheers,
Jan

… that should have read “messages_path instead of user_messages_path”

On Apr 22, 10:20 pm, Jan F. [email protected]
wrote:

<% form_for :messages, @message,
:url => user_messages_path(@user) do |f| %>

form_for [@user, @message] do

  • Hendrik

Hendrik M. wrote:

On Apr 22, 10:20�pm, Jan F. [email protected]
wrote:

� <% form_for :messages, @message,
� � � � � � � :url => user_messages_path(@user) do |f| %>

form_for [@user, @message] do

Nice! Thanks Hendrik.

Very cool, thanks a lot guys!