2016-05-11 4 views
0

私はすべてのユーザーが私のフォーラムの投稿にコメントを編集、更新、破棄できないようにしようとしています。私はすべてのユーザーが投稿を編集するのを防ぐことができましたが、私はすべてのユーザーがコメントを編集しないようにするために何をする必要があるのでしょうか。コメントの作成者に編集、更新、破棄させる方法はありますか?

私はポストコントローラにこの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 

これは簡単な解決策だと思いますが、私はまだレールでルビーを経験していません。

+0

試しはあなたのコードから、評論家の宝石 –

+0

ある変数は 'comment'、ない' @のcomment' – Santhosh

+0

おかげでたくさんべきであるように、それが見え、これは誤りでした。私はそれが何か簡単だったことを知っていた:) – Sapsford11

答えて

0

_comment.html.hamlでは、おそらくどこにも宣言されていない@commentを使用しています。 commentにそれを変更しても問題が解決しなければならない

- if comment.user_id == current_user.id 
0

インスタンス変数@comment nilを取得したため、このnil:NilClassエラーが発生します。そのため、@commentからuser_idを取得せず、このエラーが発生しました。

@comment変数をコメントに変更すると、user_idが表示されます。

+0

ありがとう、これはそれを修正しました:) – Sapsford11

関連する問題