Frage

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 %>
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top