Reputation: 974
i'm new to Ruby on Rails and i'm getting this error:
in `const_get': uninitialized constant Devise::Models::RoleId (NameError)
when i remove this ", :role_ids" from my models/user.rb the application works, but i can't assign the roles to my users..
my models/role.rb
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
scopify
end
my routes:
Rails.application.routes.draw do
resources :users
root :to => 'home#index'
devise_for :users, :skip => [:registrations, :sessions]
as :user do
get "/login" => "devise/sessions#new", :as => :new_user_session
post "/login" => "devise/sessions#create", :as => :user_session
delete "/logout" => "devise/sessions#destroy", :as => :destroy_user_session
end
end
and my models/user.rb
class User < 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 => true
end
and my UsersController:
class UsersController < 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 => { :success => 'User was successfully created.' }
else
render :action => '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 => true) if @user == current_user
redirect_to @user, :flash => { :success => 'User was successfully updated.' }
else
render :action => 'edit'
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path, :flash => { :success => '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
i followed this two tutorials: http://danielboggs.com/articles/rails-authentication-and-user-management-via-crud/ and http://danielboggs.com/articles/rails-authentication-and-user-management-continued/
Upvotes: 1
Views: 1108
Reputation: 974
I found the problem.
the solution is:
params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password, role_ids: [])
and not:
params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password, role_ids)
Upvotes: 1