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.resource :account
    users.resources :roles
    users.resources :messages

-> A link on a page (should bring up a form for a new message

  <%= link_to 'Send a message', new_user_message_path(@user, :target_id
=> %>

-> The "MessagesController" like this:

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

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

  <% form_for @message do |f| %>
      <label for='subject'>Subject:</label>
      <%= f.text_field :subject, :focus => true %>
  <% 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

.. that should have read "messages_path instead of user_messages_path"

form_for [@user, @message] do

- Hendrik

Hendrik Mans wrote:

Very cool, thanks a lot guys!