Wie verwenden Sie Active Record Enum Radio-Buttons in einem Formular?

In meine app, es ist ein Kommentar zu den Artikeln. Ich möchte den user die Möglichkeit haben zu kommentieren mit 3 verschiedenen Optionen. Um dies zu aktivieren, bin ich über eine Active Record-Enumeration. Bitte beachten Sie, dass die Kommentar-Abschnitte geschachtelt ist unter dem Artikel.

resources :articles, only: [:index, :show] do
  resources :comments
end

Migration:

class AddEnumToCommentModel < ActiveRecord::Migration
  def change
    add_column :comments, :post_as, :integer, default: 0
  end
end

Kommentar Modell:

enum post_as: %w(username, oneliner, anonymous)

Ich habe versucht, fügen Sie diese, um den Inhalt anzeigen, aber verloren. Ich vermute, dass ich auch etwas tun, mein controller aber nicht sicher.

Versucht anzeigen :

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

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

  <div class="field">
    <h3>Fill in your comment</h3>
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>

  <div class="post_as">
    <h3> Choose how you want to post your comment :</h3>
    <%= f.input :content, post_as: ???, as: :radio %>
  </div>

  <br>

  <div class="actions">
    <%= f.submit %>
  </div>

  <br>
<% end %>
InformationsquelleAutor LMo | 2014-07-31
Schreibe einen Kommentar