Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio button data not saved

I'm playing around with Form Helpers. I found some code from another SO question and thought it was pretty efficient at creating radio buttons with an elegant loop. Now that I've incorporated it, it doesn't save the data (e.g. the category value is not being saved to the project table)

Please see code below of _form.html.erb

<%= form_for(@project) do |f| %>
  <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

      <ul>
      <% @project.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="form_row">
      <label for="category">Category:</label>
      <% [ 'checklist', 'process'].each do |category| %>
      <br><%= radio_button_tag 'category', category, @category == category %>
      <%= category.humanize %>
    <% end %>
    <br>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
like image 369
orky Avatar asked Dec 05 '25 20:12

orky


1 Answers

Your radio button parameter is being created outside of the project structure. If you look at params, you'll probably see

{:category => "your_category", :project => {...project params...}}

It is because you're using the radio_button_tag instead of the regular form helper. Try this instead:

f.radio_button :category, category, :checked => (@category == category)

Also, as Justin said, make sure :category is included in the project_params in your controller.

like image 134
Paul Richter Avatar answered Dec 08 '25 11:12

Paul Richter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!