سؤال

What am I doing wrong here:

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

I'm calling it with

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

It just outputs ["yes", "no"]

هل كانت مفيدة؟

المحلول

Here's how I solved it. I found the new method collection_radio_buttons and used it to build my formbuilder method.

Now I'm calling:

= f.yes_no_radio_buttons :receiving_treatment, required: true

And my formbuilder has:

  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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top