1

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>

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm afraid i'm still a bit foggy here. Say I add method(s) to the model that returns the value of :served_in_us_army (presumably I can do something like attr_accessor :served_in_us_army). Wouldn't I still need a check in the view to see what the value is and determine if the button is checked depending on the value?

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.