2016-01-23 46 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