![](/img/trans.png)
[英]No route matches {:action=>“index”, :controller=>“comments”, :post_id=>nil} missing required keys: [:post_id]
[英]comments not saving post_id
我遇到了一個小問題(新手)。
我有2個模型:問題與評論。
評論架構:question_id,user_id,評分,評論
在“顯示”視圖上,我集成了以下表格(表格形式):
- semantic_form_for @question.reviews.build do |f|
= f.error_messages
= f.input :rating
= f.input :comments
= f.buttons
我的評論控制器的創建動作如下所示:
def create
@review = Review.new(params[:review])
@review.user_id = current_user.id
if @review.save
flash[:notice] = "Successfully created review."
redirect_to(@review.question)
else
redirect_to(@review.question)
end
end
但是,現在看來似乎並沒有將問題ID保存在question_id字段中。 它確實很好地保存了user_id。
有人知道我在做什么錯嗎? 如果您需要日志,請告訴我! 提前致謝
您需要在表單上為問題ID添加一個隱藏字段。 就像是
f.hidden_field :question_id
更具體地說,保存了user_id,因為您是在控制器中分配它的。 您需要將question_id從表單傳遞到控制器,以使其也被保存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.