2016-01-23 45 views
1

自从昨天我从权威人士(因为这太难了)转到Cancancan(它对我来说更好)之后,我正在制作一个网络应用程序(聊天类的东西)。设计和Cancancan - 如何使它工作?

我试图做一些简单的工作,如显示所有文章及其选项(显示,编辑,销毁),然后对其设置权限,以便创建此类文章的唯一用户将能够编辑或销毁它。

问题是我不明白它是如何完全实现的。 Google缺乏大量过时的示例和示例。

以下是我有:

Ability.rb- 我不知道这是否是正确的,即使

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.new # guest user (not logged in) 
    if user.admin? 
     can :manage, :all 
    else 
     can :read, :all 
    end 

    can :read, :articles 
    can :create, :articles 
    end 
end 

User.rb(设计)

class User 
    include Mongoid::Document 
    has_many :articles 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    ## Database authenticatable 
    field :username,    type: String, default: "" 
    field :email,    type: String, default: "" 
    field :encrypted_password, type: String, default: "" 

    ## Recoverable 
    field :reset_password_token, type: String 
    field :reset_password_sent_at, type: Time 

    ## Rememberable 
    field :remember_created_at, type: Time 

    ## Trackable 
    field :sign_in_count,  type: Integer, default: 0 
    field :current_sign_in_at, type: Time 
    field :last_sign_in_at, type: Time 
    field :current_sign_in_ip, type: String 
    field :last_sign_in_ip, type: String 

    ## Admin 
    field :admin, :type => Boolean, :default => false 
end 

Article.rb

class Article 
    include Mongoid::Document 
    belongs_to :user 

    field :title, type: String 
    field :content, type: String 

    default_scope -> { order(created_at: :desc) } 
end 

的index.html(显示文章 - 只有在我加入Cancancan部分)

<tbody> 
    <% @articles.each do |article| %> 
    <tr> 
     <td><%= article.title %></td> 
     <td><%= article.content %></td> 
     <td><%= link_to 'Show', article %></td> 
     <td> 
      <% if can? :update, @article %> 
      <%= link_to 'Edit', edit_article_path(article) %> 
      <% end %> 
     </td> 
     <td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
       </tr> 
      <% end %> 
      </tbody> 
+0

你到目前为止在这里看起来不错。当以管理员身份登录时,您应该能够看到编辑和销毁链接,对吧? –

+0

我没有办法测试管理员(这些帐户都不是实际的管理员^^)我如何让文章的作者能够编辑和销毁它? – Fresz

+0

好的,我检查了管理功能是否正常工作。现在 - 我如何让文章的所有者只能编辑它? – Fresz

回答

3

您需要通过类的Ability文件来定义你的权威:

#app/models/ability.rb 
class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.new # guest user (not logged in) 
    if user.admin? 
     can :manage, :all 
    else 
     can :read, :all 
    end 

    can [:credit, :edit, :update, :destroy], Article, user_id: user.id 
    end 
end 

-

#app/views/articles/index.html.erb 
<tbody> 
    <% @articles.each do |article| %> 
    <tr> 
     <td><%= article.title %></td> 
     <td><%= article.content %></td> 
     <td><%= link_to 'Show', article %></td> 
     <td><%= link_to 'Edit', article if can? :update, article %></td> 
     <td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } if can? :destroy, article %></td> 
     </tr> 
    <% end %> 
</tbody> 

顺便说一句,考虑与该第二重要的因素是Devise =认证; CanCanCan =授权:

  • 认证 =被用户登录?
  • 授权 =用户可以这样做吗?

我看到很多人发布关于“授权”与Devise,当它是完全错误的。 Devise只处理认证(用户登录?);当处理授权时,您需要使用不同的模式,利用Devise创建的user对象。

只是想指出,考虑到你在原来的帖子中提到Devise

+0

谢谢,这真的让事情变得清晰起来。 – Fresz

+0

没问题,如果您有任何其他问题,请不要犹豫,问 –

+1

不要担心我会问 - 这是我的第一个五年计划,我打算使用对我来说是新的技术......所以Ruby on Rails和MongoDB--在这两种情况下我都知道的不多。 – Fresz