# \[Rails 3\] Trouble with named routes and form\_for

**URL:** <https://www.ruby-forum.com/t/rails-3-trouble-with-named-routes-and-form-for/189056>\
**Category:** Rails\
**Created:** [May 23, 2010, 11:54am UTC](https://www.ruby-forum.com/t/rails-3-trouble-with-named-routes-and-form-for/189056 "2010-05-23T11:54:16Z")\
**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 23, 2010, 11:54am UTC](https://www.ruby-forum.com/t/rails-3-trouble-with-named-routes-and-form-for/189056/1 "2010-05-23T11:54:16Z")

</div>

Hi guys,

I’m having trouble getting named routes and form\_for to play nicely in  
quite the way I would expect. Here’s a quick summary of what I’ve got  
going on:

Named route:  
resources :thread, :class\_name =\> “forum\_thread”

Controller name:  
forum\_thread\_controller

Model object:  
forum\_thread

In both my new and edit actions I’m setting up an @thread variable:  
@thread = ForumThread.new

1.) My first attempt at writing the form\_for looked something like  
this:

\<%= form\_for @thread do |f| %\>  
…  
\<% end\>

This didn’t work because @thread tries to use a path involving the  
string “forum\_thread”, which doesn’t have a matching route and which I  
don’t want.

2.) So that’s fine, I figured I’d just use named routes. So I tried  
this:

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

This works for edit actions, but not for new actions. On new I get the  
following error:

No route matches {:action=\>“destroy”, :controller=\>“forum\_thread”,  
:id=\>#\<ForumThread id: nil, …\>}

3.) So then I tried:

\<%= form\_for @thread, :as =\> :thread, :url =\> threads\_path(@thread) do  
|f| %\>  
…  
\<% end \>

This doesn’t work for edit, and sorta works for new except it outputs  
the following HTML, which makes the respond\_to block unhappy:

....

4.) So then I tried:

\<%= form\_for @thread, :as =\> :thread, :url =\> threads\_path do |f| %\>  
…  
\<% end \>

Now everything works for new, but not for edit! (Because the ID of the  
element being edited isn’t emitted as part of the action):

So:

1. Is there some way to use a named route that uses a custom class name  
and still be able to reuse my form partial for both new and edit  
actions? Or am I stuck writing two forms?
2. Is the error I received in #2 a bug in Rails 3 or expected behavior?
3. Is the erroneous output in #3 a bug in Rails 3 or expected behavior?

My sincere thanks in advance for your help!

-Jury

---

<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 23, 2010, 11:39pm UTC](https://www.ruby-forum.com/t/rails-3-trouble-with-named-routes-and-form-for/189056/2 "2010-05-23T23:39:35Z")

</div>

I’ve also tried

\<%= form\_for @thread, :as =\> :thread %\>

and this results in the error:

undefined method `forum\_threads\_path’ for  
#\<#Class:0x00000103a45098:0x000001039575a0\>

That would seem to be a bug to me. Shouldn’t form\_for be using :as =\>  
:thread to name that route appropriately?

-M
