# Cannot grok form\_for

**URL:** <https://www.ruby-forum.com/t/cannot-grok-form-for/187916>\
**Category:** Rails\
**Created:** [May 2, 2010, 2:23am UTC](https://www.ruby-forum.com/t/cannot-grok-form-for/187916 "2010-05-02T02:23:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [May 2, 2010, 2:23am UTC](https://www.ruby-forum.com/t/cannot-grok-form-for/187916/1 "2010-05-02T02:23:35Z")

</div>

I’m stumped with form\_for(). I’ve RTFM’d until my eyeballs have bled.  
I’ve looked over examples. I still don’t get it, and each thing I try  
leads to some other error. If someone can lead me to form\_for  
enlightenment, I’d be in your debt.

I’m using RESTful, shallow routing: one User :has\_many Reports.  
A Report has three fields of importance:  
– user\_id (its parent)  
– location\_id (reference to a Location record)  
– report\_type (“graph” or “chart”)

I’m trying to write a \_report.html.erb partial that does double duty: it  
can fill in fields of a new report (report.new\_record? =\> true) as well  
as update fields of an existing report (report.new\_record? =\> false).

The \_report partial receives two inputs: report is the current report  
(either new or extant), and @user is set to the owning user.

The form\_for needs present the user with [location\_id] and [report\_type]  
fields (eventually to be replaced by pull\_down lists), and needs to  
silently fill in the [user\_id] field of the report. Then, upon  
form.submit(), it needs to ‘POST user\_reports\_path(@user.id)’ for new a  
report, or ‘PUT report\_path(report.id)’ for updating a report.

# But even my basic attempts get an error message, and I’m not sure why:

# \<% form\_for(:report, report, :url =\> {:action =\> “update”}) do |f| %\> …

# gets an error

# ActionView::TemplateError (No route matches {:action=\>“update”}) on line #12 of app/views/reports/\_report.html.erb

My reports\_controller.rb _does_ have an update method. Perhaps because  
it’s not using the HTTP PUT method? If so, how do you specify which  
HTTP method gets invoked when you click on the form.submit button?

I feel like a wimp for even asking, but can someone give me a sketch of  
what the form\_for might look like here?

Many thanks.

- ff

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [May 2, 2010, 3:55am UTC](https://www.ruby-forum.com/t/cannot-grok-form-for/187916/2 "2010-05-02T03:55:58Z")

</div>

Okay, some elucidation:

The following works when report.new\_record? is true, fails when  
report.new\_record? is false:

form\_for([@user, report])  
…

because it’s trying to call the undefined function ‘user\_report\_path’.  
(Of course it’s undefined – I’ve specified shallow routing!) So  
perhaps @Alex’s comment of [Form\_for with nested shallow route - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/206593) is  
relevant here.

So if I modify my form\_for for the new\_record vs non-new\_record, it  
looks like this (yech):

\<% form\_for((report.new\_record?)?([@user, report]):(report)) do |f| %\>  
\<%= f.submit ‘JumpUpAndDown’ %\>  
\<% end %\>

Aside from looking pretty nasty, the generated HTML is a bit suspicious:  
Shouldn’t it be a POST method when report.new\_record? is true and a PUT  
method when report.new\_record? is false?

Generated for report.new\_record? == true:

Generated for report.new\_record? == false:

What am I missing here?

- ff
