0

my envirenment:ruby:2.0.0p0, rails:3.2.13, cancan: 1.6.10, devise: 2.2.4
Hi, when i following the cancan's wiki Separate-Role-Model, and it seems didn't work for me? when i Debugging-Abilities, i find the following:

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>  

The user_id is nil.
I have already add the user_id and role_id to assignment table, question_id to user table, user_id to question table.
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   

And the view:

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

Then when i create a question but it doesn't come with an edit button. What's wrong with me? if you need more information, please tell me.

4

1 に答える 1

0

このように能力クラスを設定してみてください

class Ability                                                                                                                                                
  include CanCan::Ability                                                                                                                                    

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

問題がまだあるかどうかを確認するだけですか?次に、承認ロジックを徐々に追加して、どこが壊れているかを確認できます..

于 2013-07-28T11:11:43.257 に答える