0

我的环境:ruby:2.0.0p0, rails:3.2.13, cancan: 1.6.10, devise: 2.2.4
嗨,当我关注 cancan 的 wiki分离角色模型时,它似乎对我不起作用?当我调试能力时,我发现以下内容:

2.0.0-p0 :002 > q = Question.first
  Question Load (0.1ms)  SELECT "questions".* FROM "questions" LIMIT 1
 => #<Question id: 1, title: "问题", created_at: "2013-05-14 11:14:31", updated_at: "2013-05-14 11:14:31", content: "答案", user_id: nil>  

user_idnil。_
我已经添加user_id and role_id到分配表、question_id用户表user_id和问题表。
role.rb

class Role < ActiveRecord::Base                                                                                                                              
  attr_accessible :name                                                                                                                                      
#  has_and_belongs_to_many :users                                                                                                                            
  has_many :assignments                                                                                                                                      
  has_many :users, :through => :assignments                                                                                                                  
end

赋值.rb

class Assignment < ActiveRecord::Base                                                                                                                        
  # attr_accessible :title, :body                                                                                                                            
  belongs_to :user                                                                                                                                           
  belongs_to :role                                                                                                                                           
end 

用户.rb

class User < ActiveRecord::Base                                                                                                                              
  # Include default devise modules. Others available are:                                                                                                    
  # :token_authenticatable, :confirmable,                                                                                                                    
  # :lockable, :timeoutable and :omniauthable                                                                                                                
  devise :database_authenticatable, :registerable,                                                                                                           
         :recoverable, :rememberable, :trackable, :validatable                                                                                               

  # Setup accessible (or protected) attributes for your model                                                                                                
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :profile                                                               
  # attr_accessible :title, :body                                                                                                                            

  has_many :assignments                                                                                                                                      
  has_many :roles, :through => :assignments                                                                                                                  
  has_many :questions                                                                                                                                        

  def has_role?(role_sym)                                                                                                                                    
    roles.any? { |r| r.name.underscore.to_sym == role_sym }                                                                                                  
  end                                                                                                                                                        
end  

能力.rb

class Ability                                                                                                                                                
  include CanCan::Ability                                                                                                                                    

  def initialize(user)                                                                                                                                       
      if user.blank?                                                                                                                                         
      cannot :manage, :all                                                                                                                                   
      can :read, Question                                                                                                                                    
    elsif user.has_role?(:admin)                                                                                                                             
      can :manage, :all                                                                                                                                      
    else                                                                                                                                                     
      can :create, Question                                                                                                                                  
      can :update, Question, :active => true, :user_id => user.id                                                                                            
      can :destroy, Question, :active => true, :user_id => user.id                                                                                           
    end                                                                                                                                                      
  end                                                                                                                                                        
end   

和观点:

<% if can? :update, @question %>                                                                                                                     
   <%= link_to 'Edit', edit_question_path(question), :method => :get, :class => "btn btn-mini btn-warning" %>                                         
<% end %>  

然后当我创建一个问题但它没有edit按钮时。我怎么了?如果您需要更多信息,请告诉我。

4

1 回答 1

0

尝试像这样设置你的能力类

class Ability                                                                                                                                                
  include CanCan::Ability                                                                                                                                    

  def initialize(user)                                                                                                                                       
      can :manage, :all                                                                                                                                      
  end                                                                                                                                                        
end   

只是看看问题是否仍然存在?然后您可以逐渐添加您的授权逻辑以查看它在哪里中断..

于 2013-07-28T11:11:43.257 回答