# \[Rails 3\] Path helper not working as expected with form\_for

**URL:** <https://www.ruby-forum.com/t/rails-3-path-helper-not-working-as-expected-with-form-for/189390>\
**Category:** Rails\
**Created:** [May 31, 2010, 5:08am UTC](https://www.ruby-forum.com/t/rails-3-path-helper-not-working-as-expected-with-form-for/189390 "2010-05-31T05:08:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [May 31, 2010, 5:08am UTC](https://www.ruby-forum.com/t/rails-3-path-helper-not-working-as-expected-with-form-for/189390/1 "2010-05-31T05:08:08Z")

</div>

Hi there,

I can’t seem to get form\_for to work correctly with the URL I’m  
providing for a set of nested resources. Here’s what I’m doing in  
routes.rb:

resources :threads do  
resources :messages  
end

And then I’ve got the following form that I’m building as part of  
/threads/1/messages/1/edit. Both @thread and @message are set by the  
controller

…  
\<%= form\_for @thread, :as =\> :thread, :url =\>  
thread\_messages\_path(@thread, @message) do |f| %\>  
\<%= fields\_for :message do |m| %\>  
…  
\<% end %\>  
\<%= submit\_tag “Update Message” %\>  
\<% end %\>

This is producing the following HTML:

Notice that while the HTTP method is correctly PUT, the form action ends  
in .1 instead of /1, which is confusing the router.

Curiously, the ‘new’ case seems to be getting properly constructed,  
though I had to manually specify the :method to properly generate the  
URL:

\<%= form\_for @thread, :as =\> :thread, :url =\> thread\_messages\_path,  
:html =\> {:method =\> :post} do |f| %\>  
\<%= fields\_for :message do |m| %\>  
…  
\<% end %\>  
\<%= submit\_tag “Create Reply” %\>  
\<% end %\>

Am I doing something obviously wrong or is this a deficiency in the path  
convenience methods? Note that I am using “:as =\>” all over the place  
because the name of my model object (which is actually “Axthread”) does  
not cleanly map to a named route, and using “thread” as a model name is  
not an option.

-Mike

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [May 31, 2010, 9:40am UTC](https://www.ruby-forum.com/t/rails-3-path-helper-not-working-as-expected-with-form-for/189390/2 "2010-05-31T09:40:16Z")

</div>

So it would seem that there is huge level of subtly in how these path  
helpers behave based on how your pluralize the helper. So, what I  
should have written (and what now appears to work) was:

\<%= form\_for @thread, :as =\> :thread, :url =\>  
thread\_message\_path(@thread, @message) do |f| %\>

So that’s thread\_message\_path, not thread\_messages\_path.

However, despite trying other options, it seems I still have to specify  
the HTTP verb as part of the ‘new’ case:

\<%= form\_for @thread, :as =\> :thread, :url =\>  
thread\_messages\_path(@thread), :html =\> {:method =\> :post} do |f| %\>

Failing to specify :post results in the form thinking it should be  
PUTing instead of POSTing:

-Mike
