views:

53

answers:

2

In my main template I have the following:

 <%= render :partial => "delivery_date", :collection => @brand.delivery_dates, :locals => {:form => f} %>

However when the partial tries to use the form local variable, I get this error

Showing app/views/brands/_delivery_date.html.erb where line #2 raised:

wrong number of arguments (0 for 1)
Extracted source (around line #2):

1: <%= delivery_date.id %>
2: <%= form.text_field :name %>
3: <% new_or_existing = delivery_date.new_record? ? 'new' : 'existing' %>
4: <% prefix = "brand[#{new_or_existing}_delivery_date_attributes][]" %>
5: <% fields_for prefix, delivery_date do |dd_f| %>

Does anyone understand what this error means?

Actually I want to do

<% form.fields_for delivery_date do |dd_f| %> 

but that fails as well.

I tried replacing :locals with :locals => { :f => f } and referring to the form using f in the partial. No effect, the error then is undefined method or variable f.

...

Ok rather than asking you to debug an error, the question is also how to render fields_for in a nested partial form:

In main template:

<% form_for @object do |f| %>
    <% render :partial => 'child', :collection: @object.child %>
<% end %>

In child template:

<% form.fields_for child do |c_f| %>
    <%= c_f.text_field :foo %>
<% end %>

What is the appropriate way to pass this form object?

Sorry forgot to add environment:

Ruby 1.8.7

Rails 2.3.? (have to look up minor version, don't have the code in front of me)

(I know this is a newbie question :( )

+1  A: 

you are passing :form => f along with the partial, therefore you should refer to "f" and not "form"

just replace all instances of "form" with "f" in your partial otherwise ruby/rails will think that you are trying to call the form builder method (and that requires at least one argument, hence the error message)

stephenmurdoch
You also need to change the `:locals` hash to `{:f => f}` in that case.
Tom
Thanks! Um how does the locals hash works? :form => f that does not set the form variable? Sorry a bit confused on that.
Dmitriy Likhten
This does not work. If I use `:locals => { :f => f }` and use `f` instead of `form` in the partial, i get undefined variable error.
Dmitriy Likhten
+1  A: 

That's weird. It should work as you have it with :form or :f. If you're using Ruby 1.9, maybe there's some difference in how local variables work. I would try :locals => { "f" => f } with a string key instead of symbol and see if that helps. Also, does it work to use a simpler case like :locals => { :x => 2 }?

mckeed
:locals => { :x => 2 } works.Coencidentally I realized since I was also rendering this template in a link_to_function, it might have been that I was not looking at the error stack completely and missing where the error was comming from. I think this helped :P I will keep at it. Thanks.
Dmitriy Likhten