2014-11-05 52 views
0

我的应用程序有问题,其中有许多答案,其中有许多评论。param丢失或值为空:在Rails 4中存储具有关系的实体

不过,我试图挽救一个答案的评论时遇到问题,我收到此错误:

param is missing or the value is empty: comment 

突出显示的行,这是一个:

params.require(:comment).permit(:description) 

的图,其中评论创建的是问题的展示视图,所以在QuestionController首先我加载comment

def show 
    @answer = Answer.new 
    @comment = Comment.new 
    end 

这是该视图的样子:

<p> 
    <strong>Answers:</strong> 
</p> 
<% unless @question.answers.empty? %> 
    <ul class="list-groups"> 
    <% @question.answers.each do |answer| %> 
     <li class="list-group-item"> 
      <h4><%= answer.description %></h4> 
      <hr> 
      <%= form_for(@comment, html: { class: "form-inline" }) do |f| %> 

       <div class="form-group"> 
       <%= text_field_tag 'description', nil, placeholder: 'Comment this answer', class: "form-control" %> 
       </div> 
       <div class="form-group"> 
       <%= f.submit "Comment", class: "btn btn-default" %> 
       </div> 
       <input id="answer_id" name="answer[id]" type="hidden" value="<%= answer.id %>"> 
      <% end %> 
     </li> 
    <% end %> 
    </ul> 
<% else %> 

有什么不对?

回答

1

相反的:

<%= text_field_tag 'description', nil, placeholder: 'Comment this answer', class: "form-control" %> 

用途:

<%= f.text_field :description, placeholder: 'Comment this answer', class: "form-control" %> 

您需要使用f(的form_for的对象)结合你试图建立形式为对象(@comment)的属性,这就是为什么它没有在HTML中正确创建,因此没有在参数内部的注释键中提交。

相关问题