【问题标题】:Rails User Association with comments带有评论的 Rails 用户关联
【发布时间】:2013-06-08 22:16:32
【问题描述】:

我有一个用户脚手架 (DEVISE)、一个评论脚手架和一个电影脚手架

目前,评论已发布在电影放映页面上。

我遇到的问题是让用户创建评论。这样评论是由用户创建的。

所以如果我在movies/:show 中显示评论

我可以的

正文:<%= comment.body %> 作者:<%= comment.user.first_name %>

如何让评论属于某个用户,并且只能由该用户编辑和销毁?

请不要告诉使用before_filter :authenticate_user!, only: [:create,:destroy] 或者按照 Michael Hartl 微贴教程,因为我已经完成了这两个操作,但它们不起作用

无论如何,有谁知道我该怎么做?

谢谢

【问题讨论】:

    标签: ruby-on-rails devise comments associations


    【解决方案1】:

    首先我会向所有者显示editdestroy 链接:

    <% if comment.user == current_user %>
      <%= link_to "edit", ... %>
      <%= link_to "delete", ... %>
    <% end %>
    

    然后为了以防那些知道如何在 chrome 中使用检查元素的聪明人,我会为评论所有者做一个控制器级别的检查:

    def edit
      @comment = Comment.find(params[:id])
      if @comment.user == current_user
        @comment.update_attributes(....)
        @message = "Comment updated or created or deleted, depends on method"
      else
        @message = "It's not your comment wise guy :)"
      end
      redirect_to :back, notice: @message
    end
    

    destroy 和 update 方法相同。

    !不是可复制/粘贴的代码。

    这是我曾经做过的,效果很好,您可以使用其他方法 gem cancan https://github.com/ryanb/cancan 并为用户设置能力。

    can :edit, Comment, :user_id => user.id
    can :destroy, Comment, :user_id => user.id
    

    通过这种方式设置能力,只有所有者才能访问编辑页面和更新、销毁操作。

    【讨论】:

    • 还有update!否则,聪明的用户将能够编辑该帖子。
    • 是的,您是对的,对于更新操作,您也必须检查所有者。感谢您提及这一点。
    • 这不起作用,因为它所做的只是说如果存在 current_user,则评论是可编辑的。此外,我必须更改代码,因为使用您提供的代码,它对已登录的用户是隐藏的,就好像我注销一样,我可以看到这些操作。
    • 如果我确实使用 can :edit, Comment, :user_id => user.id 代码会去哪里?
    • can :edit 将进入能力模型,设置它需要时间,但最终效果很好......关于 current_user,你如何在评论中保存 user_id,不是吗?使用 current_user 或类似的方法将用户保存到会话中? railscasts.com/episodes/192-authorization-with-cancan
    【解决方案2】:

    关于设计助手“current_user”的情况如何?像这样:

    class Comment < ActiveRecord::Base
      belongs_to :user
    end
    
    class CommentsController < ApplicationController
      def edit
        comment = current_user.comments.where(id: params[:id]).first
        if comment.nil?
          ...
          401 error or something else (current user is not creator of this comment)
        else
         ...
        end
       end
    end
    

    您还可以在视图中检查权限:

    <% if comment.user == current_user %>
      <%= link_to "edit comment" ... %>
      <%= link_to "delete comment" ... %>
    <% end %>
    

    【讨论】:

      【解决方案3】:

      要使评论属于用户,请在您的create 操作中:

      comment = current_user.comments.new(params[:comment])
      

      使其仅对所有者可编辑/可销毁

      before_filter :find_comment, only: [:show, :edit, :update, :destroy]
      before_filter :authorize_user!, only: [:edit, :update, :destroy]
      #...
      
      private
      
        def find_comment
          @comment = Comment.find params[:id]
        end
      
        def authorize_user!
          deny_access if @comment.user != current_user # example
        end
      

      【讨论】:

        【解决方案4】:

        确保用户使用:authenticate_user! 登录是件好事,但您也必须将评论与该用户相关联。

        Devise 给你一个current_user。所以如果你的Commentbelongs_to :user和你的Userhas_many :comments写在你的CommentsController

        def new
          @comment= current_user.comments.new
        end
        
        def create
          @comment= current_user.comments.new(params[:comment])
          if @comment.save
            ...
          end
        end
        
        def edit
          @comment= current_user.comments.find(params[:id])
        end
        
        def update
          @comment= current_user.comments.find(params[:id])
          if @comment.update_attributes(params[:comment])
            ...
          end
        end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-09
          • 2017-12-04
          • 2013-06-04
          • 2017-08-27
          相关资源
          最近更新 更多