私はすべてのユーザーが私のフォーラムの投稿にコメントを編集、更新、破棄できないようにしようとしています。私はすべてのユーザーが投稿を編集するのを防ぐことができましたが、私はすべてのユーザーがコメントを編集しないようにするために何をする必要があるのでしょうか。コメントの作成者に編集、更新、破棄させる方法はありますか?
私はポストコントローラにこのbefore_actionとポストの問題を解決:私はpost_controllerにコメントbefore_actionを追加
#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"
と:
before_action :post_owner, only: [:edit, :update, :destroy]
は、これが私のshow.html.hamlです_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
これは簡単な解決策だと思いますが、私はまだレールでルビーを経験していません。
試しはあなたのコードから、評論家の宝石 –
ある変数は 'comment'、ない' @のcomment' – Santhosh
おかげでたくさんべきであるように、それが見え、これは誤りでした。私はそれが何か簡単だったことを知っていた:) – Sapsford11