devise - Why my cancan didn't work properly? -


my envirenment:ruby:2.0.0p0, rails:3.2.13, cancan: 1.6.10, devise: 2.2.4
hi, when following cancan's wiki separate-role-model, , seems didn't work me? when debugging-abilities, find following:

2.0.0-p0 :002 > q = question.first   question load (0.1ms)  select "questions".* "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 nil.
have add user_id , role_id assignment table, question_id user table, user_id 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 , :omniauthable                                                                                                                   devise :database_authenticatable, :registerable,                                                                                                                     :recoverable, :rememberable, :trackable, :validatable                                                                                                   # setup accessible (or protected) attributes 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 view:

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

then when create question doesn't come edit button. what's wrong me? if need more information, please tell me.

try set ability class

class ability                                                                                                                                                   include cancan::ability                                                                                                                                        def initialize(user)                                                                                                                                              can :manage, :all                                                                                                                                         end                                                                                                                                                         end    

just see if problem still there or not? can gradually add authorize logic see breaks ..


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -