为什么我的康康舞不能正常工作?

时间:2013-05-15 03:19:52

标签: devise ruby-on-rails-3.2 cancan

我的环境:ruby:2.0.0p0, rails:3.2.13, cancan: 1.6.10, devise: 2.2.4
嗨,当我跟随cancan的wiki Separate-Role-Model时,它似乎对我不起作用? 当我Debugging-Abilities时,我发现以下内容:

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

assgnment.rb

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

user.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  

ability.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按钮时。我怎么了?如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

尝试像这样设置你的Ability类

class Ability                                                                                                                                                
  include CanCan::Ability                                                                                                                                    

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

只是为了看问题是否还存在?然后你可以逐渐添加你的授权逻辑,看看它在哪里破解..