Rails fields_for 使用多个局部变量渲染部分,产生未定义的变量

发布于 2024-10-03 19:16:31 字数 1040 浏览 8 评论 0原文

所有,

我在标准 fields_for 设置方面遇到问题。在我的“_form”部分中,我有:

<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”部分中,我有常用字段,然后是我的测试变量:

<%= tester.to_s %>

当我删除测试器变量时,一切正常。添加测试变量后,我收到此错误:

ActionView::Template::Error(#Class:0xa1f3664>:0xa1f1bd4>的未定义局部变量或方法“tester”)

在将 fields_for 与多个局部变量一起使用时,是否还有其他人遇到过此问题?


更详细地说,我的“_comment_fields”部分如下所示:

<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”部分调用。

All,

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>

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:

ActionView::Template::Error (undefined local variable or method `tester' for #Class:0xa1f3664>:0xa1f1bd4>)

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


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>

It is only called from the "_form" partial.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜访吸血鬼 2024-10-10 19:16:31

所有,

Hakunin 都是赚钱的。我在不止一个地方调用了部分。第二个位置是我的辅助方法“link_to_add_fields”。我用它来使用 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 方法。我像这样更改了它:

# 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 调用如下所示:

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

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

All,

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.

The method looked like this:

# 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  

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  

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.

雅心素梦 2024-10-10 19:16:31

将 : 更改

<%= render :partial => 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>

为 :

<%= render 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>

我刚刚遇到了同样的问题。

Change :

<%= render :partial => 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>

to :

<%= render 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>

I just had the same problem.

对你的占有欲 2024-10-10 19:16:31

我不清楚为什么需要在表单字段中使用测试器变量。但是您能否粘贴一段代码,您如何以部分形式使用测试器变量。

我坚信

<%= tester.to_s %>

should not generate any issue as it only displays a value of that variable

I am not clear why do you need to use tester variable in form field. But can you please paste a code how are you using tester variable in partial form.

I strongly believe that

<%= tester.to_s %>

should not generate any issue as it only displays a value of that variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文