Topic: Form_for and partials....

I'm aving trouble getting the form parameter passed into the partial when using a form_for.  Here's the code I'm currently using:

<% form_for :person, :url=>{:short_name=>@competition, :action=>:start} do |f| -%>
<%= render :partial => 'register_form', :locals=>{:f=>f} -%>
<%= submit_tag 'Submit' -%>
<% end -%>

Here's the partial:
<p><label for="person_first_name">First Name</label><br/>
<%= f.text_field :first_name  %></p>

<p><label for="person_last_name">Last Name</label><br/>
<%= f.text_field :last_name  %></p>

<p><label for="person_email">Email</label><br/>
<%= f.text_field :email  %></p>


I get a "undefined local or method 'f'" error on the second line of the partial.  But I thought that's what the :locals=>{:f=>f} was supposed to deal with..

[Edit:  It looks like it might be a ZenTest Test::Rails error, as the ViewTest Fails, while the page renders in production.]
[Edit x2: Nope, not a ZenTest bug...  Created a blank project, with a single model, controller, view, partial... single view test, passes]

Last edited by NielsenE (2007-01-27 23:50:27)

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Form_for and partials....

I don't know what could be the problem, everything looks normal in the code you posted - except maybe the minus signs for the output ERB tags. It probably won't make a difference but try removing those?

<%= render :partial => 'register_form', :locals=>{:f=>f} %>
<%= submit_tag 'Submit' %>

Railscasts - Free Ruby on Rails Screencasts

Re: Form_for and partials....

No change after removing the new-line surpressing minuses...

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Form_for and partials....

Hmm, this looks like some amount of non-independent test cases...

If I run "ruby test/views/register_view_test.rb", I get the same error.  If I run "autotest -rails"
I get "You have a nil object when you didn't expect it!  The error occurd while evaluating nil.text_field" on the first pass through.  On all later autotest runs, it goes back to the first error. Grrr...


Hmm here's something else:

If I inline the entire partial, the error remains.  This seems to imply that something is failing in the form_for function and its not generating a valid object to pass into the block?


Edit:

Hmm, I've found something that makes it work, adding the model object to the form_for appears to work:

<% form_for :person, @person, :url=>{:short_name=>@competition, :action=>:start} do |f| -%>

works, while
<% form_for :person, :url=>{:short_name=>@competition, :action=>:start} do |f| -%>

doesn't.

All the RDoc's I've read, and the AWDwR v2, all show the :model_object, :url=>hash form.  Do you understand why the actual model object is required in this instance?  Is it because I needed multiple paramters in the :url hash?  I don't see what thay would change it, but....

I'm running Rails 1.2.1

Last edited by NielsenE (2007-01-28 00:54:12)

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Form_for and partials....

Do you have any plugins like simply_helpful loaded? I know that affects form_for.

Either way, it sounds like a bug.

Railscasts - Free Ruby on Rails Screencasts