Rails:fields_for与索引?

有没有一种方法(或拉开类似的function)做一个fields_for_with_index

例:

 <% f.fields_for_with_index :questions do |builder, index| %> <%= render 'some_form', :f => builder, :i => index %> <% end %> 

这部分被呈现需要知道什么是当前的索引是在fields_for循环。

这实际上是一个更好的方法,更紧密地遵循Rails文档:

 <% @questions.each.with_index do |question,index| %> <% f.fields_for :questions, question do |fq| %> # here you have both the 'question' object and the current 'index' <% end %> <% end %> 

来自: http : //railsapi.com/doc/rails-v3.0.4/classes/ActionView/Helpers/FormHelper.html#M006456

也可以指定要使用的实例:

  <%= form_for @person do |person_form| %> ... <% @person.projects.each do |project| %> <% if project.active? %> <%= person_form.fields_for :projects, project do |project_fields| %> Name: <%= project_fields.text_field :name %> <% end %> <% end %> <% end %> <% end %> 

答案很简单,因为解决scheme是在Rails中提供的。 你可以使用f.options参数。 所以,在您的渲染_some_form.html.erb

索引可以通过访问:

 <%= f.options[:child_index] %> 

你不需要做任何事情。


更新:似乎我的答案还不够清楚…

原始HTML文件:

 <!-- Main ERB File --> <% f.fields_for :questions do |builder| %> <%= render 'some_form', :f => builder %> <% end %> 

呈现的子表单:

 <!-- _some_form.html.erb --> <%= f.options[:child_index] %> 

从Rails 4.0.2开始,FormBuilder对象中包含一个索引:

http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormHelper/fields_for

例如:

 <%= form_for @person do |person_form| %> ... <%= person_form.fields_for :projects do |project_fields| %> Project #<%= project_fields.index %> ... <% end %> ... <% end %> 

结帐渲染部分的集合 。 如果您的要求是模板需要迭代数组并为每个元素呈现子模板。

 <%= f.fields_for @parent.children do |children_form| %> <%= render :partial => 'children', :collection => @parent.children, :locals => { :f => children_form } %> <% end %> 

这会渲染“_children.erb”并将局部variables“children”传递给模板进行显示。 迭代计数器将自动使用名称为partial_name_counter的模板。 在上面的例子中,模板将被喂给children_counter

希望这可以帮助。

Rails 4+和Rails 3(下面有补丁)

 <%= form_for @person do |person_form| %> <%= person_form.fields_for :projects do |project_fields| %> <%= project_fields.index %> <% end %> <% end %> 

对于Rails 3的支持

要让f.index在Rails 3中工作,你需要添加一个猴子补丁到你的项目初始化器中,以便将这个function添加到fields_for

 # config/initializers/fields_for_index_patch.rb module ActionView module Helpers class FormBuilder def index @options[:index] || @options[:child_index] end def fields_for(record_name, record_object = nil, fields_options = {}, &block) fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options? fields_options[:builder] ||= options[:builder] fields_options[:parent_builder] = self fields_options[:namespace] = options[:namespace] case record_name when String, Symbol if nested_attributes_association?(record_name) return fields_for_with_nested_attributes(record_name, record_object, fields_options, block) end else record_object = record_name.is_a?(Array) ? record_name.last : record_name record_name = ActiveModel::Naming.param_key(record_object) end index = if options.has_key?(:index) options[:index] elsif defined?(@auto_index) self.object_name = @object_name.to_s.sub(/\[\]$/,"") @auto_index end record_name = index ? "#{object_name}[#{index}][#{record_name}]" : "#{object_name}[#{record_name}]" fields_options[:child_index] = index @template.fields_for(record_name, record_object, fields_options, &block) end def fields_for_with_nested_attributes(association_name, association, options, block) name = "#{object_name}[#{association_name}_attributes]" association = convert_to_model(association) if association.respond_to?(:persisted?) association = [association] if @object.send(association_name).is_a?(Array) elsif !association.respond_to?(:to_ary) association = @object.send(association_name) end if association.respond_to?(:to_ary) explicit_child_index = options[:child_index] output = ActiveSupport::SafeBuffer.new association.each do |child| options[:child_index] = nested_child_index(name) unless explicit_child_index output << fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block) end output elsif association fields_for_nested_model(name, association, options, block) end end end end end 

我无法看到一个体面的方式来通过Rails提供的方式来做到这一点,至less不在-v3.2.14

@Sheharyar Naseer提到了可以用来解决问题的选项哈希值,但并不像我所看到的那样。

我做了这个=>

 <%= f.fields_for :blog_posts, {:index => 0} do |g| %> <%= g.label :gallery_sets_id, "Position #{g.options[:index]}" %> <%= g.select :gallery_sets_id, @posts.collect { |p| [p.title, p.id] } %> <%# g.options[:index] += 1 %> <% end %> 

要么

 <%= f.fields_for :blog_posts do |g| %> <%= g.label :gallery_sets_id, "Position #{g.object_name.match(/(\d+)]/)[1]}" %> <%= g.select :gallery_sets_id, @posts.collect { |p| [p.title, p.id] } %> <% end %> 

在我的情况下, g.object_name返回一个string像这样"gallery_set[blog_posts_attributes][2]"为第三个字段呈现,所以我只是匹配该string中的索引,并使用它。


其实一个冷却器(也许干净?)的方式是传递一个lambda,并将其称为增量。

 # /controller.rb index = 0 @incrementer = -> { index += 1} 

而在视图中

 <%= f.fields_for :blog_posts do |g| %> <%= g.label :gallery_sets_id, "Position #{@incrementer.call}" %> <%= g.select :gallery_sets_id, @posts.collect { |p| [p.title, p.id] } %> <% end %> 

我知道这有点晚了,但是我最近不得不这样做,你可以得到fields_for的索引

 <% f.fields_for :questions do |builder| %> <%= render 'some_form', :f => builder, :i => builder.options[:child_index] %> <% end %> 

我希望这个对你有用 :)

如果你想控制索引检查index选项

 <%= f.fields_for :other_things_attributes, @thing.other_things.build do |ff| %> <%= ff.select :days, ['Mon', 'Tues', 'Wed'], index: 2 %> <%= ff.hidden_field :special_attribute, 24, index: "boi" %> <%= end => 

这会产生

 <select name="thing[other_things_attributes][2][days]" id="thing_other_things_attributes_7_days"> <option value="Mon">Mon</option> <option value="Tues">Tues</option> <option value="Wed">Wed</option> </select> <input type="hidden" value="24" name="thing[other_things_attributes][boi][special_attribute]" id="thing_other_things_attributes_boi_special_attribute"> 

如果表单被提交,params将包括类似的东西

 { "thing" => { "other_things_attributes" => { "2" => { "days" => "Mon" }, "boi" => { "special_attribute" => "24" } } } 

我不得不使用索引选项让我的多下拉列表工作。 祝你好运。