0

In my Rails project, I am trying to set up user authentication and user roles using the following Gems:

  • Devise
  • Cancan
  • Rolify

I had Devise up and working perfectly, but then when I went to set up CanCan and Rolify, I get this error in the browser:

NameError in Home#index

Showing C:/Sites/JustManage/app/views/devise/menu/_registration_items.html.erb where line #1 raised:

undefined local variable or method `rolify' for #<Class:0x3f48660>
Extracted source (around line #1):

1: <% if user_signed_in? %>
2: 
3:   <li>
4:      <%= link_to('Edit registration', edit_user_registration_path) %>
Trace of template inclusion: app/views/layouts/application.html.erb

Rails.root: C:/Sites/JustManage

user.rb

class User < ActiveRecord::Base
  rolify

  # 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, :subdomain, :first_name, :last_name

  # Make sure certain attributes are unique
  validates_uniqueness_of :email, :case_sensitive => false
end

role.rb

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => :users_roles
  belongs_to :resource, :polymorphic => true

  scopify
end

rolify.rb

Rolify.configure do |config|
  config.use_dynamic_shortcuts
end

Let me know if you need me to post contents from any of my other files!

Keenan Payne
  • 796
  • 2
  • 15
  • 32

2 Answers2

6

I had the same issue. Restarting my server fixed it!

'When in doubt, ctrl-c out!'

zenben1126
  • 685
  • 1
  • 7
  • 25
1

Is it safe to assume that you installed and subsequently required the rolify gem properly?...

Please verify that rolify is listed when you run gem list and that you have added require 'rolify' at the top of your user.rb file.

grenierm5
  • 186
  • 4
  • 14
  • I added the `rolify` gem to my `gemfile`, then I generated a role using `rails generate rolify:role`. – Keenan Payne Dec 04 '13 at 16:39
  • When I added `require 'rolify` to the top of my `user.rb` file, I got this error: `cannot load such file -- rolify` – Keenan Payne Dec 04 '13 at 16:39
  • You need to run `bundle install` after you add the rolify gem to your Gemfile. – grenierm5 Dec 04 '13 at 16:40
  • I ran `bundle install` but unfortunately I'm still getting that same `cannot load such file...` error. I mean I have all of the Rolify files in my application, and I did a `rake db:migrate` so all of the Rolify database tables should be there... – Keenan Payne Dec 04 '13 at 16:41
  • 1
    Are you using ActiveRecord STI? https://github.com/EppO/rolify/wiki/FAQ#when-i-start-rails-using-server-console-whatever-i-get-this-error If not, please tell me which ruby/rails/rolify versions you're using. – grenierm5 Dec 04 '13 at 19:06
  • I'm not really sure what ActiveRecord STI is. I know I'm not using Mongoid ORM however. I'm using Ruby 1.9.3, Rails 3.2.1, and Rolify 3.2. – Keenan Payne Dec 04 '13 at 19:17