我正在创建一个答案模型并保存它的查询部分,但是,它的匿名部分不是。
这是我的答案模型
class Answer < ActiveRecord::Base
has_many :comments, dependent: :destroy
belongs_to :question
attr_accessible :anonymous, :answer, :commenter, :votes, :comments_attributes
accepts_nested_attributes_for :comments
end
这是我的表格。
<%= form_for([@question, @answer]) do |f| %>
<p>
<%= f.label :answer %>
<%= f.text_area :answer, :cols => "50", :rows => "30"%>
</p>
<p>
<%= check_box_tag(:anonymous)%>
<%= label_tag(:anonymous, "Anonymous")%>
</p>
<p>
<%= f.submit "Submit Answer" %>
</p>
<% end %>
当我检查命令提示符的POST请求时,最初答案和匿名都有值,答案可能有字符串“aw; oeigh; aioewhg”和匿名值为1.但是,当它执行实际创建时,匿名获取零和答案得到“a; oewigh; oih。”这是为什么
这是我的控制器,如果它有任何帮助。谢谢!
def create
@answer = @question.answers.new(params[:answer])
if @answer.anonymous == true
@answer.commenter = "Anonymous"
else
@answer.commenter = current_user.username
end
if @answer.save
redirect_to question_path(@question)
else
redirect_to questions_path
end
end
答案 0 :(得分:0)
可能是因为您的匿名匿名不在答案范围内
尝试
f.check_box :anonymous
代替
<%= check_box_tag(:anonymous)%>