我試圖創建一個博客,我有3個模型post
,user
,comment
。當我創建comment
時,comment.post_id
是nil
。我究竟做錯了什麼?爲什麼user_id未分配comment_id?
comments_controller
def new
@comment = Comment.new
end
def edit
@post = Post.find(params[:post_id])
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
@comment.user_id = current_user
redirect_to post_path(@post)
end
編輯
post.rb
has_many :comments, dependent: :destroy
belongs_to :user
comment.rb
belongs_to :user
belongs_to :post
user.rb
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
在這些模型中顯示您的關聯。 – 2015-02-23 17:00:47
更新後的問題 – vveare138 2015-02-23 17:03:49