开发者

Error when rendering a partial (RoR) passing the form as a local variable

开发者 https://www.devze.com 2022-12-24 00:01 出处:网络
In my main template I have the following: <%= render :partial => \"delivery_date\", :collection => @brand.delivery_dates, :locals => {:form => f} %>

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开发者_JAVA百科| %> 

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 :( )


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 }?


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号