【发布时间】:2026-01-24 12:10:01
【问题描述】:
我正在尝试阻止所有用户编辑、更新和销毁在我的论坛中发布的 cmet。我设法阻止所有用户编辑帖子,但我无法弄清楚我需要做什么来阻止所有用户编辑 cmets。
我在 post 控制器中用这个 before_action 解决了 post 问题:
before_action :post_owner, only: [:edit, :update, :destroy]
这是我的 show.html.haml:
#post_content
%h1= @post.title
%p= @post.content
#comments
%h2
= @post.comments.count
Comments
= render @post.comments
- if user_signed_in?
%h3 Reply to thread
= render'comments/form'
%br/
%hr/
%br
- if @post.user_id == current_user.id
= link_to "Edit", edit_post_path(@post), class: "button"
= link_to "Delete", post_path(@post), method: :delete, data: { confirm: "Are you sure you want to do this?"}, class: "button"
我在 post_controller 中为 cmets 添加了 before_action 并在 _comment.html.haml 中尝试了这个:
.comment.clearfix
.content
%p.comment_content= comment.comment
%p.comment_author= comment.user.email
%br/
%br/
%br/
%br/
- if @comment.user_id == current_user.id
= link_to "Edit", edit_post_comment_path(comment.post, comment), class: "button"
= link_to "Delete", [comment.post, comment], method: :delete, data: { confirm: "Are you sure?"}, class: "button"
但是我收到以下错误:
undefined method `user_id' for nil:NilClass
我认为这是一个简单的解决方案,但我对 ruby on rails 还没有那么丰富的经验。
【问题讨论】:
-
从您的代码看来,变量应该是
comment,而不是@comment -
非常感谢,这是错误。我知道这很简单:)
标签: ruby-on-rails ruby comments userid