title: How to assign roles to a user with devise, cancan and rolify?
body: <p>i'm new to Ruby on Rails and i'm getting this error:</p>
  
  <pre><code>in `const_get': uninitialized constant Devise::Models::RoleId (NameError)
  </code></pre>
  
  <p>when i remove this ", :role_ids" from my models/user.rb the application works, but i can't assign the roles to my users..</p>
  
  <p>my models/role.rb</p>
  
  <pre><code>class Role &lt; ActiveRecord::Base
      has_and_belongs_to_many :users, :join_table =&gt; :users_roles
      belongs_to :resource, :polymorphic =&gt; true
  
      scopify
  end
  </code></pre>
  
  <p>my routes:</p>
  
  <pre><code>Rails.application.routes.draw do
      resources :users
      root :to =&gt; 'home#index'
  
      devise_for :users, :skip =&gt; [:registrations, :sessions]
  
      as :user do
          get "/login" =&gt; "devise/sessions#new", :as =&gt; :new_user_session
          post "/login" =&gt; "devise/sessions#create", :as =&gt; :user_session
          delete "/logout" =&gt; "devise/sessions#destroy", :as =&gt; :destroy_user_session
      end
  end
  </code></pre>
  
  <p>and my models/user.rb</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
      rolify
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :role_ids
      validates :email, :uniqueness =&gt; true
  end
  </code></pre>
  
  <p>and my UsersController:</p>
  
  <pre><code>class UsersController &lt; ApplicationController
      load_and_authorize_resource
      def index
          @users = User.all
      end
  
      def show
          @user = User.find(params[:id])
      end
  
      def new
          @user = User.new
      end
  
      def edit
          @user = User.find(params[:id])
      end
  
      def create
          @user = User.new(user_params)
  
          if @user.save
              redirect_to @user, :flash =&gt; { :success =&gt; 'User was successfully created.' }
          else
              render :action =&gt; 'new'
          end
      end
  
      def update
          @user = User.find(params[:id])
  
          if params[:user][:password].blank?
              params[:user].delete(:password)
              params[:user].delete(:password_confirmation)
          end
  
          params[:user][:role_ids] ||= []
  
          if @user.update_attributes(user_params)
              sign_in(@user, :bypass =&gt; true) if @user == current_user
              redirect_to @user, :flash =&gt; { :success =&gt; 'User was successfully updated.' }
          else
              render :action =&gt; 'edit'
          end
      end
  
  
      def destroy
          @user = User.find(params[:id])
          @user.destroy
          redirect_to users_path, :flash =&gt; { :success =&gt; 'User was successfully deleted.' }
      end
  
      private
      def user_params
          params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password)
          if can? :manage, User
              params.require(:user).permit(:username, :email, :role_ids, :salt, :encrypted_password)
          else
              params[:user].delete(:role_ids)
              params[:user].delete(:email)
          end
      end
  end
  </code></pre>
  
  <p>i followed this two tutorials:
  <a href="http://danielboggs.com/articles/rails-authentication-and-user-management-via-crud/" rel="nofollow">http://danielboggs.com/articles/rails-authentication-and-user-management-via-crud/</a>
  and
  <a href="http://danielboggs.com/articles/rails-authentication-and-user-management-continued/" rel="nofollow">http://danielboggs.com/articles/rails-authentication-and-user-management-continued/</a></p>