1

cancan について非常に混乱しています。mysql データベースから cancan のロールを取得しようとしました。show/edit/destroy のようなアクションを持たないコントローラーを使用しています (非 RESTful コントローラーと呼ばれていますか?)。ユーザー テーブルに Role_id 列があり、ロール テーブルに id と rolename があります。

私が得るのはこのエラーだけです:

未定義のメソッド `find_by_name' #

Ability.rb:
 class Ability
      include CanCan::Ability
      def initialize(user)
        can :manage, :all if user.role? :admin
    end
    end

そして、ここに私の 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

そして、ここで私が取り組んでいるコントローラー:

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
4

1 に答える 1

0

find_by_nameActiveRecord クラスメソッドです。( Active Record Query Interface Guide を参照してください)インスタンス メソッドのように呼び出しています。

これを変更してみてください:

def role?(role)
    return !!self.role.find_by_name(role.to_s.camelize)
end

このようなものに:

def role?(role_name)
    return self.role.present? && self.role.name == role_name.to_s
end

もちろん、Ruby では、returnとはどちらselfもオプションです。

于 2012-12-12T06:45:13.927 に答える