评论功能:nil:NilClass 的未定义方法“评论”
Comment function: undefined method `comment' for nil:NilClass
我想为我的 rails 应用程序创建一个评论功能。因此只有 current_user
或管理员(我使用 active_admin)才能删除他的评论。但我很难弄清楚这一点,因为我的方法似乎指向 nil
。有人可以帮帮我吗?
comments_controller.rb
class CommentsController < ApplicationController
before_action :correct_user, only: :destroy
def create
@post =Post.find(params[:post_id])
@comment =@post.comments.create(params[:comment].permit(:name, :body))
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment= @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
private
def correct_user
@user= User.find(current_user.id)
redirect_to(root_url) unless current_user.id == @post.comment.user.id
end
end
在我的 correct_user 方法中出现了未定义的注释,所以我已经尝试添加
@post = Post.find(params[:post_id])
@comment= @post.comments.find(params[:id])
并尝试了不同的方法来制作这个 运行。
Comment.rb
class Comment < ApplicationRecord
belongs_to :post
end
Post.rb
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true
validates :user, presence: true
validates :user_id, presence: true
has_attached_file :image #, :styles => { :medium => "300x300>", :thumb =>
"100x100>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
User.rb
class User < ApplicationRecord
has_many :posts
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
PS:我想通过一个 before 操作来执行此操作,然后在删除 link.
周围使用一个 if 语句
#correct_user
中的 @post
是 nil
,因为它首先在 #destroy
中设置。此外,您的 Comment
模型当前与 User
模型没有关系,@post.comment.user.id
将不起作用,因为 #user
也将是未定义的。
要更正此问题,请在 Comment
和 User
之间添加关系,仅在正确的用户调用 destroy
操作时才调用 @comment.destroy
。
试试这个,
def destroy
@comment.destroy
redirect_to post_path(@post)
end
private
def correct_user
@comment = Comment.find(params[:id])
@post = @comment.try(:post)
redirect_to(root_url) unless current_user.id == @post.try(:user_id)
end
在 params[:id]
中,我们正在获取评论的 ID。此外,此 @comment.try(:post)
和 @post.try(:user_id)
仅在具有您问题中提到的关联时才有效。
Comment Model
belongs_to :post
Post Model
belongs_to :user
我想为我的 rails 应用程序创建一个评论功能。因此只有 current_user
或管理员(我使用 active_admin)才能删除他的评论。但我很难弄清楚这一点,因为我的方法似乎指向 nil
。有人可以帮帮我吗?
comments_controller.rb
class CommentsController < ApplicationController
before_action :correct_user, only: :destroy
def create
@post =Post.find(params[:post_id])
@comment =@post.comments.create(params[:comment].permit(:name, :body))
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment= @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
private
def correct_user
@user= User.find(current_user.id)
redirect_to(root_url) unless current_user.id == @post.comment.user.id
end
end
在我的 correct_user 方法中出现了未定义的注释,所以我已经尝试添加
@post = Post.find(params[:post_id])
@comment= @post.comments.find(params[:id])
并尝试了不同的方法来制作这个 运行。
Comment.rb
class Comment < ApplicationRecord
belongs_to :post
end
Post.rb
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true
validates :user, presence: true
validates :user_id, presence: true
has_attached_file :image #, :styles => { :medium => "300x300>", :thumb =>
"100x100>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
User.rb
class User < ApplicationRecord
has_many :posts
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
PS:我想通过一个 before 操作来执行此操作,然后在删除 link.
周围使用一个 if 语句#correct_user
中的 @post
是 nil
,因为它首先在 #destroy
中设置。此外,您的 Comment
模型当前与 User
模型没有关系,@post.comment.user.id
将不起作用,因为 #user
也将是未定义的。
要更正此问题,请在 Comment
和 User
之间添加关系,仅在正确的用户调用 destroy
操作时才调用 @comment.destroy
。
试试这个,
def destroy
@comment.destroy
redirect_to post_path(@post)
end
private
def correct_user
@comment = Comment.find(params[:id])
@post = @comment.try(:post)
redirect_to(root_url) unless current_user.id == @post.try(:user_id)
end
在 params[:id]
中,我们正在获取评论的 ID。此外,此 @comment.try(:post)
和 @post.try(:user_id)
仅在具有您问题中提到的关联时才有效。
Comment Model
belongs_to :post
Post Model
belongs_to :user