Search code examples
ruby-on-railsrubyform-helpers

How do you get my rails radio_button_tag to save the data on form submission?


I am newer to rails and thought I would add a radio button set to a form to enforce user choice. I am able to get the form loaded and select one of the options, but upon posting via the submit button I get a model error for the field needing to be present (something I am wanting to validate). Below is my form code for the radio buttons.

<div class="field">
  <div>
  <%= f.label "Type" %>
  </div>
  <div class="radio-inline">
    <%= radio_button_tag :ptype, "Question" %><%= label_tag :ptype_question, 'Question' %>
  </div>
  <div class="radio-inline">
    <%= radio_button_tag :ptype, "Problem" %><%= label_tag :ptype_problem, 'Problem' %>
  </div>
  <div class="radio-inline">
    <%= radio_button_tag :ptype, "Idea" %><%= label_tag :ptype_idea, 'Idea' %>
  </div>
  <div class="radio-inline">
    <%= radio_button_tag :ptype, "Praise" %><%= label_tag :ptype_praise, 'Praise' %>
  </div>
</div></br>

I've whitelisted :ptype in the params within the controller, so I know that isn't the issue, and when I make the field just a plain text_field I am able to submit just fine, so I am thinking I messed something up with the multiple options, but haven't quite been able to figure it out even after checking a few other posts.

I also noticed it is getting the data in the logging, but it isn't indented like the other elements which are:

--- !ruby/hash:ActionController::Parameters
utf8: "✓"
authenticity_token: 7uAQ/Wx4...
post: !ruby/hash:ActionController::Parameters
  title: Packers
  content: Rock!
  category: Football
ptype: Question
commit: Post
controller: posts
action: create

Again, thinking it's a simple fix since the text_field posts fine, but still too new to figure out quite what is wrong. Thanks in advance!


Solution

  • Apply form object to radio button like this:

    f.radio_button
    

    it will submit without any mess.