【发布时间】:2014-02-22 15:56:20
【问题描述】:
我正在尝试保存评论的评论文本部分,但由于某种原因无法保存。 我正在检查我的服务器输出,并设置了注释属性,但实际保存时,结果显示为 NIL。
原来有 "comment"=>{"comment"=>"hello dude"}, "commit"=>"Sbmit Comment"
但是在保存中它保存了 NIL。
这是我的 cmets 表格
<div class="container">
<% if user_signed_in? %>
<%= form_for([@answer, @comment]) do |f| %>
<p>
<%= f.label :comment %>
<%= f.text_area :comment, :cols => "50", :rows => "30"%>
</p>
<p>
<%= f.submit "Submit Comment" %>
</p>
<% end %>
<% else %>
<p> <em>You must be signed in to comment</em> </p>
<% end %>
</div>
这是我的 cmets 控制器
class CommentsController < ApplicationController
def create
@answer = Answer.find(params[:answer_id])
@comment = @answer.comments.new(params[:comments])
@comment.writer = current_user.username
@comment.save
redirect_to question_path(@answer)
end
end
这是我的模型。
class Comment < ActiveRecord::Base
belongs_to :answer
attr_accessible :anonymous, :comment, :writer, :votes
end
这是我的答案模型
class Answer < ActiveRecord::Base
has_many :comments, dependent: :destroy
belongs_to :question
attr_accessible :anonymous, :answer, :commenter, :votes, :comments_attributes
end
有什么想法吗?
编辑:我使用了 params[:comment],但是,它说我不能批量分配属性来回答,即使 answer 具有 attr_accessible: :cmets_attributes
【问题讨论】:
标签: ruby-on-rails