3
votes

I am using SimpleForm 3.2.1 with Rails 4. I am trying to convert a rails form into a simple_form. Here is the code that works for radio buttons with the regular rails form:

  <ul class="list-radios list-multiplier">
    <% @organization_plans.each_with_index do |plan, index| %>

      <li>
        <div class="radio custom-radio">
          <%= f.radio_button :plan_id, plan.id, id: "field-rad#{index}", data: {price: plan.total_price} %>

          <label class="form-label" for='field-rad<%= index %>'>
            <span></span>

            <%= plan.description %>
          </label>
        </div><!-- /.radio -->
      </li>
    <% end %>
  </ul><!-- /.list-radios -->

I have tried converting it to SimpleForm with the following code:

  <ul class="list-radios list-multiplier">
    <% @organization_plans.each_with_index do |plan, index| %>

      <li>
        <div class="radio custom-radio">
          <%= f.input :plan_id, value: plan.id, input_html: {id: "field-rad#{index}"}, data: {price: plan.total_price}, label: false, as: :radio %>

          <label class="form-label" for='field-rad<%= index %>'>
            <span></span>

            <%= plan.description %>
          </label>
        </div><!-- /.radio -->
      </li>
    <% end %>
  </ul><!-- /.list-radios -->

However, when I open the page in the browser I get the error:

No input found for radio

How do I get this form to start working with simple_form?

2

2 Answers

2
votes

According to SimpleForm source code it has not default radio input.

Documentation of this gem suggest few ways to use radio buttons:

  1. <%= f.input :plan, as: :radio_buttons %>
  2. <%= f.input :plan, as: :boolean %> (with some additional value handle)
  3. You can write your own custom input, f.e. named radio and put in folder app/inputs. Read more about custom inputs
2
votes

There's nothing wrong with using the regular rails helpers. You can nest them inside the input if you'd like consistent wrappers. It can look something like

= f.input :plan_id, label: false do 
  ul
    - @organization_plans.each do |plan|
      li
        = f.radio_button :plan_id, plan.id
        = f.label :plan_id, plan.description, value: plan.id

Of course, you may also use the collection if you don't need too much customization

= f.input :plan_id, collection: @organization_plans.pluck(:id, :description), as: :radio_buttons, label: false