1
votes

I have a Rails form helper and want to add a css-class to a radio button input field.

            <span class="input-nile">
              <%= f.radio_button :nile_administration, class: 'input__field input__field-nile radio' %>
              <label class="radio-label">Nile Administration</label>
            </span>

The output is

           <span class="input-nile">
              <input type="radio" value="{:class=>&quot;input__field input__field-nile radio&quot;}" name="user[nile_administration]" id="user_nile_administration_classinput__field_input__field-nile_radio">
              <label class="radio-label">Nile Administration</label>
            </span>

I want to achieve that the input tag of type radio get the css class "radio" like the following:

            <span class="input-nile">
              <input type="radio" value="" name="user[nile_administration]" class="input__field input__field-nile radio" id="user_nile_administration_classinput__field_input__field-nile_radio">
              <label class="radio-label">Nile Administration</label>
            </span>

How can i do that?

4

4 Answers

2
votes

The issue is with the place of the colon

replace

<%= f.radio_button :nile_administration, class: 'input__field input__field-nile radio' %>

with

<%= f.radio_button :nile_administration , :class => 'input__field input__field-nile radio' %>
1
votes

Try this:

<%= f.radio_button :nile_administration, '', '', class: 'input__field input__field-nile radio' %>

As mentioned in the docs, radio_button takes its options as the 4th argument:

radio_button(object_name, method, tag_value, options = {})
0
votes

I think this is work for you:

<span class="input-nile">
  <%= f.radio_button :nile_administration,"",class: 'input__field input__field-nile radio', id: 'user_nile_administration_classinput__field_input__field-nile_radio'%>
  <label class="radio-label">Nile Administration</label>
</span>
0
votes

You can set the custom id, classes, and other attributes for the f.radio_button as follows. Because it only takes 3 arguments. That is why using brackets.

 <%= f.radio_button :entity_type, 'practice', { checked: true, id: 'entity_type_practice', class: "custom-control-input entity-radio-button" } %>