0

I have the following code:

<%= f.collection_radio_buttons :access_type_id, AccessType.all, :id, :name, group_label_method: 'Access type' %>

and it gives me:

enter image description here

using the following HTML:

<fieldset>
...
  <span>
    <label for="log_file_access_type_id_1" name="log_file[access_type_id]">
      <input id="log_file_access_type_id_1" name="log_file[access_type_id]" type="radio" value="1">
      <label class="collection_radio_buttons" for="log_file_access_type_id_1">
        public
      </label>
    </label>
  </span>
  <span>
    <label for="log_file_access_type_id_2" name="log_file[access_type_id]">
      <input id="log_file_access_type_id_2" name="log_file[access_type_id]" type="radio" value="2">
      <label class="collection_radio_buttons" for="log_file_access_type_id_2">
        protected
      </label>
    </label>
  </span>
  <span>
    <label for="log_file_access_type_id_3" name="log_file[access_type_id]">
      <input id="log_file_access_type_id_3" name="log_file[access_type_id]" type="radio" value="3">
      <label class="collection_radio_buttons" for="log_file_access_type_id_3">
        private
      </label>
    </label>
  </span>
...
</fieldset>

I want to add label for the radio buttons group. I have try using group_label_method: 'Access type' but nothing changed.

Is there a way to add such label using simple_form methods or I should just added as plain HTML?

1 Answer 1

2

try this

simple form http://rubydoc.info/github/plataformatec/simple_form/SimpleForm/FormBuilder:collection_radio_buttons

 collection_radio_buttons(:access_type_id, AccessType.all, :id, :name_with_initial) do |b|
   b.label(:"data-value" => b.value) { b.radio_button + b.text }
 end
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting undefined method name_with_initial' for #<AccessType:0x00000006298f28>`
@gotqn thats because that is not a method callable on your AccessType model. you could just use :name. This snippet was copied from the docs for collection_radio_buttons
Where do you put this, in the haml?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.