【发布时间】:2015-06-07 18:18:27
【问题描述】:
你好,乐于助人的好心人。
当我尝试提交新评论时,我收到“nil:NilClass”错误的“未定义方法 `cmets”。 它突出显示了用户控制器的这一部分:
def create
@comment = @user.comments.build(comment_params)
@comment.user = current_user
我的 cmets 控制器:
class CommentsController < ApplicationController
def show
@comment = Comment.find(params[:id])
end
def create
@comment = @user.comments.build(comment_params)
@comment.user = current_user
if @comment.save
redirect_to users_show_path, notice: 'Comment submitted!'
else
render 'users/show'
end
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
end
private
def comment_params
params.require(:comment).permit(:comment, :username)
end
end
我的用户模型包括 has_many :cmets 并且我的 cmets 模型有 belongs_to :user
我需要去哪里纠正这个错误?谢谢!
【问题讨论】:
-
一点点搜索会有很长的路要走..
标签: ruby-on-rails forms methods comments