0
我基本上試圖複製這裏提供的示例(http://edgeguides.rubyonrails.org/getting_started.html)與名稱略有更改。每個post
可能有多個comment
,但我在提交評論時遇到了一些麻煩。 Error page when trying to create a post繼續獲取未定義的方法'標題',而我沒有任何方法稱爲標題
我能想到的最接近的是我對post
對象,要求title
領域存在的驗證。 我見過類似問題的解決方案是確保在控制器內爲方法設置了正確的私有/公共範圍,但似乎並非如此。
這裏是我的評論控制器
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
redirect_to post_path(@post)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
發佈您的模型以及。他們應該列出關係,否則你的@ post.comments聲明將會出錯。此外,請向我們顯示您的表單,以便我們知道是否實際發送了post_id。 – bkunzi01