Rails fields_for 使用多个局部变量渲染部分,产生未定义的变量
所有,
我在标准 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有,
Hakunin 都是赚钱的。我在不止一个地方调用了部分。第二个位置是我的辅助方法“link_to_add_fields”。我用它来使用 javascript 添加字段。
该方法如下所示:
请注意,这不允许将任何局部变量传递给 render 方法。我像这样更改了它:
现在我的 _form 部分中的 link_to_add_fields 调用如下所示:
...我可以动态地将字段添加到我的表单中并传递其他本地变量。希望这能帮助其他人。
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:
Notice that this does not allow any locals to be passed to the render method. I changed it like so:
Now my link_to_add_fields call in my _form partial looks like this:
...and I can dynamically add fields to my form AND pass additional locals. Hopefully, this will help someone else out.
将 : 更改
为 :
我刚刚遇到了同样的问题。
Change :
to :
I just had the same problem.
我不清楚为什么需要在表单字段中使用测试器变量。但是您能否粘贴一段代码,您如何以部分形式使用测试器变量。
我坚信
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
should not generate any issue as it only displays a value of that variable