0
votes

I'm learning how to create forms in Ruby on Rails, but I can't figure out how to get radio buttons to save when I submit a form. Here's my form:

<%= form_for :post, url: posts_path do |f| %>
    <p>
        <%= f.label :title %><br>
        <%= f.text_field :title %>
    </p>

    <p>
        <%= f.label :body %><br>
        <%= f.text_area :body %>
    </p>

    <p>
        <%= f.label :radio %><br>

        <%= f.label :radio, "Yes"%>
        <%= f.radio_button(:radio, "true")%> 

        <%= f.label :radio, "No"%>
        <%= f.radio_button(:radio, "false")%>
    </p>

     <p>
        <%= f.submit %>
    </p>

After submitting a form as a test, I check the results both on the website and in the console using SQLite. The radio attribute of my posts model is empty (value of nil), but the other attributes are filled in properly. I have already checked that the radio attribute is a string. If there's a better way to store values with radio buttons, that would be appreciated. Here's the code I use to display the results, but I don't think it will help because radio is blank when I check it with SQLite.

<% if post.radio == "true"%>
    <p>
        Yes!
    </p>
    <%else%>
    <p>

        No!
    </p>
<%end%>

I've already tried using radio_button_tag, but rails doesn't recognize it. I've also tried it without the parentheses, just true and false (not strings), etc. Thank you!

1
:radio wasn't whitelisted in the post params! Thank you so much!Will O.

1 Answers

0
votes

Adding :radio into the whitelisted values inside your post params will fix the problem. Doing that will allow the value to save to the database.