Search code examples
ruby-on-railsformsradio-buttonbootstrap-form-helper

Rails Bootstrap_form - Radio buttons not rendering label name in the show page


I have a form built with the bootstrap_form gem for ruby. within the form I have some radio buttons. the form renders perfectly with the correct labeling in the edit page of the form, however once I save the form and go to the "show" page to see the results, the inputs for the radio buttons are only showing the value of the radio button (which are numbers) and not the custom label name I have assigned. How can I make the custom label appear instead of the value in the show page?

Here is my code:

_form.html.erb:

  <%= f.form_group :gender, label: { text: "Gender" } do %>
    <%= f.radio_button :gender, 0, label: "Female", checked: true, inline: true  %>
    <%= f.radio_button :gender, 1, label: "Male", inline: true  %>
  <% end %>

  <%= f.form_group :nationality, label: { text: "Nationality" } do %>
    <%= f.radio_button :nationality, 0, label: "Kuwaiti", checked: true, inline: true  %>
    <%= f.radio_button :nationality, 1, label: "Non-Kuwaiti", inline: true  %>
  <% end %>

show.html.erb

    <p><strong>Full Name:</strong>&nbsp;&nbsp;<%= @profile.name %></p>
    <p><strong>Civil ID no:</strong>&nbsp;&nbsp;<%= @profile.civil %></p>
    <p><strong>Gender:</strong>&nbsp;&nbsp;<%= @profile.gender %></p>
    <p><strong>Nationality:</strong>&nbsp;&nbsp;<%= @profile.nationality %></p>
    <p><strong>Mobile no:</strong>&nbsp;&nbsp;<%= @profile.mobile %></p>
    <p><strong>Personal Email:</strong>&nbsp;&nbsp;<%= @profile.personal_email %></p>

Thanks for the help in advance!

Update:-

New form code:-

<%= f.form_group :gender, label: { text: "Gender" } do %>
    <%= f.radio_button :gender, 1  %>
    <%= f.label 'Female', inline: true, checked: true  %>
    <%= f.radio_button :gender, 0  %>
    <%= f.label 'Male', inline: true  %>
  <% end %>

Tried this suggestion but still getting the same problem, only the the radio buttons are no longer in line and have incorrect formatting.


Solution

  • You need a lookup table

    You have saved numeric values that correspond to the radio button. So, when you display the variable in the show.htm.erb it is showing the numeric values retrieved from the database record. This makes sense. But you need the string associated with the number, which requires a lookup table

    Creating labels in the form does not create custom labels for your field. To create custom field labels, you would need to setup i18n localizations for your activerecord models, which is a good solution, but also one that will take some time and learning curve.

    Creating a hash-based lookup table.

    Active Hash gem

    You could write your own lookup helper or use this gem which simplifies implementing hash-based lookup tables.

    Example

    # in app/models/person.rb
    class Gender < ActiveHash::Base
      self.data = [
        {:id => 0, :gender => "Female"},
        {:id => 1, :gender => "Male"}
      ]
    end
    

    From their spec

    We wrote ActiveHash so that we could use simple, in-memory, ActiveRecord-like data structures that play well with Rails forms...

    Build error in gem (Update)

    I just checked an it seems their build has an error. Probably best to avoid for now.


    Using view helpers to translate boolean values to strings (Added)

    You could implement helpers your /helpers/profile_helper.rb. I call them "lookups," but that is probably not standard.

    For each of the boolean you need to translate to strings create a helper

    #profile_helper.rb  
    # Each of your booleans
    def gender_to_s(value)
        value ? "Male" : "Female"
    end
    
    def nationality_to_s(value)
        value ? "Kuwaiti" : "Non-Kuwaiti"
    end
    
    
    # Generic true/false conversion
    def boolean_to_s(value)
        value ? "Yes" : "No"
    end
    

    Note: I would name the helpers consistently to match the field name, so in your view it is very obvious which "to_s" to invoke.

    In your view

    <p><strong>Gender:</strong>&nbsp;&nbsp;<%= gender_to_s(@profile.gender)  %></p>
    

    Note: If you wanted a single view helper, this could be implemented in application_helper.rb and include a passed parameter "type" as a switch control, but I think the most straightforward solution is to create a view helper for each field.