Search code examples
ruby-on-railsherokuactiveadmincancancancancan

Deployed rails app to Heroku not working


I've created a rails app using Active Admin on Nitrous.io and all is working in that dev environment. I am using Devise/CanCanCan for authentication/authorisation

When I push to heroku, and try and access Active Admin, I get the following error:

Started GET "/admin" for 91.226.23.198 at 2014-09-06 21:15:49 +0000                                                                                                                                                            
Processing by Admin::ProductsController#index as HTML                                                                                                                                                                          
NoMethodError (undefined method `include?' for nil:NilClass):                                                                                                                                                                  
app/models/user.rb:8:in `role?'                                                                                                                                                                                              
app/models/ability.rb:6:in `initialize'   

My user model is like so:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, 
         :recoverable, :rememberable, :trackable, :validatable

  def role?(r)
      role.include? r.to_s
    end
end

Ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
      if user.role? :administrator
        can :manage, :all
      elsif user.role? :moderator
          can :manage, Product
          can :manage, User, :id => user.id
              cannot :destroy, User
          #can :read, ActiveAdmin::Page, :name => "Dashboard"
          can :read, ActiveAdmin::Page, :name => "Contact Us"
          can :read, ActiveAdmin::Page, :name => "About x"
          can :read, ActiveAdmin::Page, :name => "FAQ"
          #can :manage, [Page, Unit, Category, News]
    else
        can :read, Product
        can :manage, User, :id => user.id
            cannot :destroy, User
        #can :read, ActiveAdmin::Page, :name => "Dashboard"
        can :read, ActiveAdmin::Page, :name => "Contact Us"
        can :read, ActiveAdmin::Page, :name => "About x"
        can :read, ActiveAdmin::Page, :name => "FAQ"
    end
end
end

What am I doing wrong?

I've tried rebooting the heroku server multiple times.

Thanks for your help guys.

Cheers!


Solution

  • Ok finally got a solution to this one. The issue was the default user created by Active Admin didn't include a role, so I seeded a user with a role = 'administrator' and was able to login with this user and it all worked.

    Thanks for all the help.