你如何在Rails Formbuilders中循环一个集合?

时间:2013-09-19 15:46:17

标签: ruby-on-rails forms formbuilder

我在这里做错了什么:

def radio_button(label, *args)
  options = args.extract_options!
  collection = options[:collection]
  options.delete :collection
  # put back into args to pass to super
  args << options

  collection.each do |item|
    label(label, class: "radio-inline") do
      super(label, item, *args) do
        item.to_s.humanize
      end
    end          
  end
end

我用

调用它
= f.radio_button :receiving_treatment, collection: ["yes", "no"], required: true

它只输出[&#34;是&#34;,&#34;没有&#34;]

1 个答案:

答案 0 :(得分:0)

以下是我如何解决它。我找到了新方法collection_radio_buttons并用它来构建我的formbuilder方法。

现在我正在打电话:

= f.yes_no_radio_buttons :receiving_treatment, required: true

我的formbuilder有:

  def yes_no_radio_buttons(method, *args)
    collection_radio_buttons(method, ["yes", "no"], :to_s, "humanize", *args)
  end

  def collection_radio_buttons(method, collection, value_method, text_method, *args)
    super(method, collection, value_method, text_method, *args) do |b|
      b.label(class: "radio-inline") { b.radio_button(*args) + b.text }
    end
  end
相关问题