Ruby on Rails Saturday, May 1, 2010

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
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment