2014-11-05 26 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中正確創建,因此沒有在參數內部的註釋鍵中提交。

相關問題