Rails:带有索引的 fields_for?
是否有一种方法(或实现类似功能的方法)来执行fields_for_with_index
?
示例:
<% f.fields_for_with_index :questions do |builder, index| %>
<%= render 'some_form', :f => builder, :i => index %>
<% end %>
正在渲染的部分需要知道 fields_for
循环中的当前索引是什么。
Is there a method (or way to pull off similar functionality) to do a fields_for_with_index
?
Example:
<% f.fields_for_with_index :questions do |builder, index| %>
<%= render 'some_form', :f => builder, :i => index %>
<% end %>
That partial being rendered needs to know what the current index is in the fields_for
loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
答案很简单,因为 Rails 中提供了解决方案。您可以使用
f.options
参数。因此,在渲染的_some_form.html.erb
中,索引可以通过以下方式访问:
您不需要执行任何其他操作。
更新:看来我的回答不够清楚...
原始HTML文件:
渲染的子表单:
The answer is quite simple as the solution is provided within Rails. You can use
f.options
params. So, inside your rendered_some_form.html.erb
,Index can be accessed by:
You don't need to do anything else.
Update: It seems that my answer wasn't clear enough...
Original HTML File:
Rendered Sub-Form:
从 Rails 4.0.2 开始,FormBuilder 对象中现在包含索引:
https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for
例如:
As of Rails 4.0.2, an index is now included in the FormBuilder object:
https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for
For example:
下面的答案是多年前发布的,对于现代方法,请参阅:
https://stackoverflow.com/a/22640703/105403
这实际上是一个更好的方法,更紧密地遵循 Rails 文档:
从:
http://railsapi.com/doc/ Rails-v3.0.4/classes/ActionView/Helpers/FormHelper.html#M006456
The answer below was posted many years ago, for a modern approach see:
https://stackoverflow.com/a/22640703/105403
This would actually be a better approach, following Rails documentation more closely:
From:
http://railsapi.com/doc/rails-v3.0.4/classes/ActionView/Helpers/FormHelper.html#M006456
适用于 Rails 4+
Monkey Patch For Rails 3 支持
要使
f.index
在 Rails 3 中工作,您需要向您的应用程序添加一个 Monkey 补丁项目初始值设定项将此功能添加到fields_for
For Rails 4+
Monkey Patch For Rails 3 Support
To get
f.index
to work in Rails 3 you need to add an monkey patch to your projects initializers to add this functionality tofields_for
查看渲染部分集合。如果您的要求是模板需要迭代数组并为每个元素渲染子模板。
这将渲染“_children.erb”并将局部变量“children”传递给模板进行显示。迭代计数器将自动提供给模板,其名称格式为
partial_name_counter
。在上面的示例中,模板将被输入children_counter
。希望这有帮助。
Checkout Rendering a collection of partials. If your requirement is that a template needs to iterate over an array and render a sub template for each of the elements.
This will render “_children.erb“ and pass the local variable 'children' to the template for display. An iteration counter will automatically be made available to the template with a name of the form
partial_name_counter
. In the case of the example above, the template would be fedchildren_counter
.Hope this helps.
我看不到通过 Rails 提供的方法来做到这一点的好方法,至少在 -v3.2.14 中没有
@Sheharyar Naseer 引用了可用于解决问题的选项哈希,但据我所知还不够看看他似乎建议的方式。
我就是这么做的=>
或者
在我的例子中,
g.object_name
返回一个像这样的字符串"gallery_set[blog_posts_attributes][2]"
对于渲染的第三个字段,所以我只匹配该字符串中的索引,使用它。实际上,一种更酷(也许更干净?)的方法是传递 lambda 并调用它来递增。
以及视图中的
I can't see a decent way to do this through the ways provided by Rails, at least not in -v3.2.14
@Sheharyar Naseer makes reference to the options hash which can be used to solve the problem but not as far as I can see in the way he seems to suggest.
I did this =>
or
In my case
g.object_name
returns a string like this"gallery_set[blog_posts_attributes][2]"
for the third field rendered so I just match the index in that string and use it.Actually a cooler (and maybe cleaner?) way to do it is to pass a lambda and call it to increment.
And the in the view
添加到 child_index 的 fields_index:0
Added to fields_for child_index: 0
我知道这有点晚了,但我最近不得不这样做,你可以像这样获取 fields_for 的索引,
我希望这有帮助:)
I know that this is a bit late but I recently had to do this you can get the index of the fields_for like this
I hope that this helps :)
如果您想控制索引,请查看
index
选项这将产生“
如果提交表单,参数将包括类似
我必须使用索引选项才能使我的多下拉菜单工作”的 内容。祝你好运。
If you want to have control over the indexes check out the
index
optionThis will produce
If the form is submitted, params will include something like
I had to use the index option to get my multi-dropdowns to work. Good luck.