在我的应用程序中,有一篇关于文章的评论部分.我希望用户能够使用3种不同的选项发表评论.要激活这个,我使用的是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 %>