Reputation: 5206
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 :( )
Upvotes: 1
Views: 1426
Reputation: 9818
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 }
?
Upvotes: 1
Reputation: 34603
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)
Upvotes: 1