Form helpers and partial

Hi,

I'm trying to use a partial in my form (Rails 2.3.4), and the docs I see say I can do this:

edit.html.erb: <% form_for(@course) do |f| %>   <%= render :partial => f %>   <p><%= f.submit "Update" %></p> <% end %>

_form.html.erb: <p>Title: <%= f.text_field :title %></p>

But when I go to the edit page I get an error:

undefined local variable or method `f' for #<ActionView::Base:0x465873c>

If I change the render line to be:   <%= render :partial => "form", :locals => {:f => f} %>

it works, but the latest updates say I don't need to do that.

Am I missing something?

Thanks.

John T. wrote:

Hi,

I'm trying to use a partial in my form (Rails 2.3.4), and the docs I see say I can do this:

edit.html.erb: <% form_for(@course) do |f| %>   <%= render :partial => f %>   <p><%= f.submit "Update" %></p> <% end %>

_form.html.erb: <p>Title: <%= f.text_field :title %></p>

Well, slight update. I found that if I change my form partial to be:

  <%= form.text_field :title %>

it works. I can live with that.

But.. reading the updates to rails and forms and partials, it says you can shortcut it more by doing:    <%= render @course %>

I found that Rails will look for the partial named: _course.html.erb. Ok, I copied my form partial, but it doesn't like any of my 'form helper' variables - f.text_field, form.text_field, or course.text_field. Is this possible?

thanks again

John T. wrote:

> edit.html.erb: > <% form_for(@course) do |f| %> > <%= render :partial => f %> > <p><%= f.submit "Update" %></p> > <% end %>

> _form.html.erb: > <p>Title: <%= f.text_field :title %></p>

Well, slight update. I found that if I change my form partial to be:

<%= form.text_field :title %>

it works. I can live with that.

Correct. The magical partial variable has the same name as the partial itself

But.. reading the updates to rails and forms and partials, it says you can shortcut it more by doing: <%= render @course %>

I found that Rails will look for the partial named: _course.html.erb. Ok, I copied my form partial, but it doesn't like any of my 'form helper' variables - f.text_field, form.text_field, or course.text_field. Is this possible?

You would need to pass (via locals etc.) the form builder object.

Fred