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

**URL:** <https://www.ruby-forum.com/t/restful-routes-how-to-code-the-create-form-correctly/135534>\
**Category:** Rails\
**Created:** [April 22, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/restful-routes-how-to-code-the-create-form-correctly/135534 "2008-04-22T22:43:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 22, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/restful-routes-how-to-code-the-create-form-correctly/135534/1 "2008-04-22T22:43:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 22, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/restful-routes-how-to-code-the-create-form-correctly/135534/2 "2008-04-22T23:20:47Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 22, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/restful-routes-how-to-code-the-create-form-correctly/135534/3 "2008-04-22T23:22:20Z")

</div>

… that should have read “messages\_path instead of user\_messages\_path”

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 23, 2008, 2:05am UTC](https://www.ruby-forum.com/t/restful-routes-how-to-code-the-create-form-correctly/135534/4 "2008-04-23T02:05:09Z")

</div>

On Apr 22, 10:20 pm, Jan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> \<% form\_for :messages, @message,  
> :url =\> user\_messages\_path(@user) do |f| %\>

form\_for [@user, @message] do

- Hendrik

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 23, 2008, 9:05am UTC](https://www.ruby-forum.com/t/restful-routes-how-to-code-the-create-form-correctly/135534/5 "2008-04-23T09:05:43Z")

</div>

Hendrik M. wrote:

> On Apr 22, 10:20ï¿½pm, Jan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ï¿½ \<% form\_for :messages, @message,  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ :url =\> user\_messages\_path(@user) do |f| %\>
> 
> form\_for [@user, @message] do

Nice! Thanks Hendrik.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 23, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/restful-routes-how-to-code-the-create-form-correctly/135534/6 "2008-04-23T12:36:37Z")

</div>

Very cool, thanks a lot guys!
