产生未定义的变量

产生未定义的变量

本文介绍了railsfields_for渲染具有多个局部变量的局部变量,产生未定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我在使用标准fields_for安装程序时遇到问题.在我的"_form"部分中,我有:

I am experiencing a problem with a standard fields_for setup. In my "_form" partial I have:

<div class="comment_list">
  <%= f.fields_for :comments do |cf| %>
    <%= render :partial => 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>
  <% end %>

  <%= link_to_add_fields "Add a comment", f, :comments %>
</div>

在"_comment_fields"部分中,我具有通常的字段,然后是测试变量:

In the "_comment_fields" partial, I have the usual fields and then my test variable:

<%= tester.to_s %>

当我删除测试器变量时,一切正常.一旦添加测试变量,就会出现此错误:

When I remove the tester variable, everything works well. As soon as I add the test variable, I get this error:

在与多个本地人一起使用fields_for时,是否还有其他人遇到过此问题?

Has anyone else ran into this problem when using a fields_for with multiple locals?

要详细说明,我的"_comment_fields"部分看起来像这样:

To elaborate a bit more, my "_comment_fields" partial looks like this:

<div class="comment dynamic_field">
  <span class="comment_content"><%= f.text_field :content, :class => "comment_content" %></span>
  <%= tester.to_s %>
  <%= link_to_remove_fields "remove", f %>
</div>

只能从"_form"部分调用它.

It is only called from the "_form" partial.

推荐答案

全部

Hakunin 赚了钱.我在多个地方都叫局部.第二点是在我的助手方法"link_to_add_fields"中.我使用它来使用javascript添加字段.

Hakunin was on the money. I was calling the partial in more than one spot. The second spot was in my helper method "link_to_add_fields." I use this to add fields using javascript.

方法如下:

# generates add fields on a dynamic form
def link_to_add_fields(name, f, association, locals={})
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields = f.fields_for(association, new_object,
                      :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", :f => builder)
  end

  link_to(name, "#", :class => "dynamic_add", 'data-association' => "#{association}",
                                            'data-content' => "#{fields}")
end

请注意,这不允许将任何本地变量传递给render方法.我是这样更改的:

Notice that this does not allow any locals to be passed to the render method. I changed it like so:

# generates add fields on a dynamic form
def link_to_add_fields(name, f, association, locals={})
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields = f.fields_for(association, new_object,
                      :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", locals.merge!(:f => builder))
  end

  link_to(name, "#", :class => "dynamic_add", 'data-association' => "#{association}",
                                            'data-content' => "#{fields}")
end

现在我在_form部分中的link_to_add_fields调用看起来像这样:

Now my link_to_add_fields call in my _form partial looks like this:

<%= link_to_add_fields "Add a comment", f, :comments, :tester => true %>

...然后我可以将字段动态添加到表单中,并传递其他本地变量.希望这可以帮助其他人.

...and I can dynamically add fields to my form AND pass additional locals. Hopefully, this will help someone else out.

这篇关于railsfields_for渲染具有多个局部变量的局部变量,产生未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:40