在我的应用程序中,文章下有一个评论区。我希望用户可以使用三个不同的选项进行评论。为了实现这一点,我正在使用Active Record Enum。请注意,评论部分是嵌套在文章下面的。
resources :articles, only: [:index, :show] do
resources :comments
end
迁移:
class AddEnumToCommentModel < ActiveRecord::Migration
def change
add_column :comments, :post_as, :integer, default: 0
end
end
评论模型:
enum post_as: %w(username, oneliner, anonymous)
我尝试将这个添加到内容视图中,但失败了。我猜我还需要在控制器中做一些事情,但不确定。
尝试的视图:
<%= 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 %>