简体   繁体   English

Railsfields_for枚举ID

[英]Rails fields_for enumerate with id

If I have a standard fields_for method such as: 如果我有一个标准的fields_for方法,例如:

<%= f.fields_for :exptypes do |builder| %>
    <%= render 'exptype_fields', :f => builder %>
<% end %>

Is there such a way to get: 有没有这样的方法:

<%= f.fields_for :exptypes do |builder, i| %>
    <%= render 'exptype_fields', :f => builder, :i => i %>
<% end %>

So that a unique number ( i ) is passed to the form? 以便将唯一数字( i )传递给表单?

What I want to do is make unique div tags inside partials so I can run javascript on specific partials in the code. 我想做的是在partials中创建唯一的div标签,这样我就可以在代码中的特定partials上运行javascript。

You're in luck - Rails does this already, automatically. 您很幸运-Rails已经自动做到了。 From http://guides.rubyonrails.org/layouts_and_rendering.html under 3.4.6 Local Variables 来自http://guides.rubyonrails.org/layouts_and_rendering.html,位于3.4.6局部变量下

Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by _counter. Rails还使计数器变量在该集合所调用的部分中可用,该变量以该集合的成员命名,后跟_counter。 For example, if you're rendering @products, within the partial you can refer to product_counter to tell you how many times the partial has been rendered. 例如,如果要渲染@products,则可以在part内引用product_counter来告诉您该部分已渲染了多少次。 This does not work in conjunction with the as: :value option. 不能与as :: value选项一起使用。

In your case, this is probably going to be exptype_fields_counter . 就您而言,这可能是exptype_fields_counter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM