Ruby On Rails: CanCan with Devise error 'undefined method `find_by_name' for #<Role:0x304ad18>-Collection of common programming errors
I’m very confused of cancan, I tried to get roles for cancan out of my mysql database. I’m using a controller which got no actions like show/edit/destroy (it’s called a Non RESTful Controller I think?). I got a Role_id column in my user table and id and rolename in my roles table.
All I get is this error:
undefined method `find_by_name’ for #
Ability.rb:
class Ability
include CanCan::Ability
def initialize(user)
can :manage, :all if user.role? :admin
end
end
And here is my User.rb:
User.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :firstname, :lastname, :birthday
# attr_accessible :title, :body
validates_uniqueness_of :username
belongs_to :role
def role?(role)
return !!self.role.find_by_name(role.to_s.camelize)
end
end
Role.rb:
class Role < ActiveRecord::Base
has_many :users
attr_accessible :users, :name
end
And here the controller I’m working in:
class WsdlController < ApplicationController
authorize_resource :class => false
$liga_id = 456
$liga_short = "bl1"
$saison = 2012
def connect
@client = Savon::Client.new("http://www.openligadb.de/Webservices/Sportsdata.asmx?WSDL")
@output = ""
end
def get_all_for_new_saison
if @client.nil?
connect
end
#get_teams_by_league_saison
#get_matchdata_by_league_saison
end
end
-
find_by_name
is anActiveRecord
class method. (See the Active Record Query Interface Guide) You are invoking it like an instance method.Try changing this:
def role?(role) return !!self.role.find_by_name(role.to_s.camelize) end
to something like this:
def role?(role_name) return self.role.present? && self.role.name == role_name.to_s end
Of course, in ruby, both
return
andself
are optional.
Originally posted 2013-11-09 22:47:57.