Ramy
Ramy

Reputation: 21261

having trouble with :checked radio_button - rails

I'm sorry for posting another question about this same piece of code but I'm such a newbie to rails it's painful. My question is, I'd like to get a third radio button to be selected when the default value in the DB is present. In other words, i have three buttons "true", "false", and "either". If nothing is selected yet or if the user has selected the "either" button, i'd like that selection to be displayed. Currently if the user chooses "true" or "false", then that selection is reflected properly and as expected. It's just that third option being selected that's not being reflected when the form is saved or brought up again. Here's my code:

<div class="new-partner-form">
<%= form_for [:admin, matching_profile.partner, matching_profile],  :html => {:id => "edit_profile", :multipart => true} do |f| %>
  <div class="rounded-block semi-wide clear">
    <h4>Military Service</h4>
    <%= f.radio_button :served_in_us_army, false %>
    <%= label :served_in_us_army, 'NO', {:style => 'display:inline'} %>
    <%= f.radio_button :served_in_us_army, true %>
    <%= label :served_in_us_army, 'YES', {:style => 'display:inline'} %>
    <%= f.radio_button :served_in_us_army, " ", { :checked => (:served_in_us_army.nil? or :served_in_us_army.blank? ? true : false) } %>
    <%= label :served_in_us_army, 'Either', {:style => 'display:inline'} %>
    <%= f.error_message_on :served_in_us_army %>
  </div>

EDIT: I should note that also tried the ":checked" option without the curly braces. Still no luck.

Not sure if this is the best solution, but here is how i've solved the issue (i needed the field value not the column name):

<div class="rounded-block semi-wide clear">
  <h4>Military Service</h4>
  <%= f.radio_button :served_in_us_army, false %>
  <%= label :served_in_us_army, 'NO', {:style => 'display:inline'} %>
  <%= f.radio_button :served_in_us_army, true %>
  <%= label :served_in_us_army, 'YES', {:style => 'display:inline'} %>
  <%= f.radio_button :served_in_us_army, " ", :checked =>  matching_profile.served_in_us_army.nil? %>
  <%= label :served_in_us_army, "Either", {:style => 'display:inline'} %>
  <%= f.error_message_on :served_in_us_army %>
</div>

Upvotes: 1

Views: 429

Answers (1)

Michael Durrant
Michael Durrant

Reputation: 96484

I would recommend you not do this in the view. It's business logic.

Try to do it in the controller or even better in the the model.

In this case you might want the controller for the new and (particularly) the edit actions and perhaps create an instance variable for that information. You can select the values from the database, add a manual option, have an if-then, whatever to set the object correctly.

Best of all (always) is to try and address this at the model level. You could have model method(s) that look at the existing value for Military Service and then return information about that - even a true/false value as booleans are great. Just make sure to prepare the attribute(s) correctly (to match the db) before doing .save 's

Upvotes: 1

Related Questions