2013-05-15 3 views
0

my envirenment: ruby:2.0.0p0, rails:3.2.13, cancan: 1.6.10, devise: 2.2.4
Привет, когда я следую wiki Separate-Role-Model cancan, и, похоже, он не работает для меня? , когда я Debugging-Abilities, я нахожу следующее:Почему мой cancan не работал должным образом?

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_id является nil.
Я уже добавил 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

а также Вид:

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

Тогда, когда я создать вопрос, но он не приходит с edit кнопку. Что со мной не так? если вам нужна дополнительная информация, пожалуйста, скажите мне.

+0

В вашем классе «Способность» попробуйте просто поместить can: manage,: all без каких-либо операторов if или check .., чтобы увидеть, есть ли проблема в ваших операторах if или нет .. – simo

ответ

0

Попробуйте установить свой класс Ability как этот

class Ability                                     
    include CanCan::Ability                                  

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

просто чтобы увидеть, если проблема все еще существует или нет? то вы можете постепенно добавить свою авторизационную логику, чтобы увидеть, где она ломается.

Смежные вопросы