ruby on rails - How can I add a role to a user? (devise + cancancan) -


as have written in title don't know how can add roles user. made tutorial .

my session_controller.rb

class sessionscontroller < applicationcontroller   skip_authorization_check    def update     id = params[:id].to_i     session[:id] = user::roles.has_key?(id) ? id : 0     flash[:success] = "your new role #{user::roles[id]} set!"    end end 

my ability.rb

class ability   include cancan::ability    def initialize(user)        user ||= user.new       if user.role?(:admin)        can :manage, :all      else        can :read, :all      end    end end 

and in user.rb model

  roles = {0 => :guest, 1 => :admin}    attr_reader :role    def initialize(role_id = 0)     @role = roles.has_key?(role_id) ? roles[role_id] : roles[0]   end    def role?(role_name)     role == role_name   end 

now able on demo page


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -