Tim Fletcher
Tim Fletcher

Reputation: 7396

"Other" text field for radio button collection in Rails

I have a model with a field, candidate_name which is displayed on a form as a collection of radio buttons. One of the radio buttons is an 'other' option which displays the candidate_name_other virtual attribute. When entering a value into candidate_name_other I want to store that value in the candidate_name field of the database.

I can almost get this to work but when editing the record I can't get the candidate_name radio to be selected correctly when candidate_name_other is populated.

Model:

class Question < ActiveRecord::Base

  attr_accessor :candidate_name_other

  def candidate_name_other=(value)
    self.candidate_name = value if candidate_name == 'other'
  end

  def candidate_name_other
    candidate_name
  end
end

Form field:

<%= f.collection_radio_buttons(:candidate_name, [['candidate', 'Candidates'], ['option', 'Options'], ['other', 'Other']], :first, :second) do |b| %>
  <% b.label do %>
    <%= b.radio_button %><%= b.text %>
  <% end %>
<% end %>
<%= f.text_field :candidate_name_other %>

So when I've selected the 'other' radio, entered an alternate value, saved and loaded the edit action, I'd like to see this:

enter image description here

But what I'm actually seeing is this:

enter image description here

Which makes sense because in order for the radio to be selected as 'Other', the value would need to be 'other' rather than 'foobar'. How do I send the correct 'other' value to candidate_name for display?

Upvotes: 3

Views: 2157

Answers (3)

Dende
Dende

Reputation: 584

I had several radio buttons and one should have been attached to a text field. This is what I figured out. I made a text input field near a radio-button which is hidden:

    = form.radio_button :database, 'hidden_radio', style: "display:none"
    = form.input :whatever, as: :text, label: false, class: "form-control"
  Something

Then I assigned a value of text input to the radio button with JavaScript and checked the button:

:javascript
  var re = document.getElementById("id_of_text_field");
  var cd = document.getElementById("id_of_radio_button");
  re.onclick = function(){
    cd.setAttribute("checked", "checked");
    cd.value = re.value;
  };

If the text is entered to the text field - it is saved to the database. If another radio button is checked - its value is saved to the database. Hope it helps some newbies like me

Upvotes: 1

L.D
L.D

Reputation: 1329

I have a very similar problem described here: Using Rails 3.2 and Rails form - don't know how to display an input text field when a radio button is checked true and hide it otherwise

I attempted to use something suggested here: Mixing radio buttons and text_field For the first answer I was not able to extend the solution suggested to an working solution for me and the second answer was too complex to even give it a try.

Based on your code I was able to fix mine. Please look at my own answer for my issue and maybe will work for you. I guess may solution is also aligned with the first answer you already received earlier.

Upvotes: 0

nathany
nathany

Reputation: 549

It's possible to pass :checked => true to radio_button, so you could have some custom logic that determines if the radio should be checked if name isn't in the collection of other values. Seems kinda messy, but workable.

Another option would be to make a form object using "include ActiveModel::Model" (a bit closer to the Django way). In that class, have separate attributes for candidate_name and candidate_name_other to make the form logic simple. That class could then be responsible for storing data as a single field in your real ActiveRecord model and extracting it back into the two attributes.

Upvotes: 0

Related Questions