Witam, tym razem mam problem z zastosowaniem CanCana. Zgodnie z railscastem zainstalowałęm gem i stworzyłem model Ability.
#models/ability.rb
[code]class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role == “admin”
can :manage, Post
else
can [:new,:index,:show,:create], Post
end
end
end[/code]
Sprawdzać uprawnienia chcę w controlerze postów:
[code]class PostsController < ApplicationController
before_filter :require_user, :only => [ :new ]
load_and_authorize_resource
…[/code]
W chwili gdy wchodzę na localhost/posts/
Dostaję błąd:
[quote]NameError in PostsController#index
uninitialized constant ActiveRecord::Relation
vendor/rails/activesupport/lib/active_support/dependencies.rb:442:in load_missing_constant' vendor/rails/activesupport/lib/active_support/dependencies.rb:77:in
const_missing’
/var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/model_adapters/active_record_adapter.rb:102:in override_scope' vendor/rails/activerecord/lib/active_record/attribute_methods.rb:211:in
any?’
/var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/model_adapters/active_record_adapter.rb:102:in each' /var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/model_adapters/active_record_adapter.rb:102:in
any?’
/var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/model_adapters/active_record_adapter.rb:102:in override_scope' /var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/model_adapters/active_record_adapter.rb:89:in
database_records’
/var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/model_additions.rb:23:in accessible_by' /var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/controller_resource.rb:81:in
load_collection’
/var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/controller_resource.rb:34:in load_resource' /var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/controller_resource.rb:25:in
load_and_authorize_resource’
/var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/controller_resource.rb:10:in send' /var/lib/gems/1.8/gems/cancan-1.6.0/lib/cancan/controller_resource.rb:10 vendor/rails/activesupport/lib/active_support/callbacks.rb:182:in
call’
vendor/rails/activesupport/lib/active_support/callbacks.rb:182:in evaluate_method' vendor/rails/activesupport/lib/active_support/callbacks.rb:166:in
call’
vendor/rails/actionpack/lib/action_controller/filters.rb:225:in call' vendor/rails/actionpack/lib/action_controller/filters.rb:629:in
run_before_filters’
vendor/rails/actionpack/lib/action_controller/filters.rb:615:in call_filters' vendor/rails/actionpack/lib/action_controller/filters.rb:610:in
perform_action_without_benchmark’
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in
measure’
vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' vendor/rails/actionpack/lib/action_controller/rescue.rb:136:in
perform_action_without_caching’
vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in perform_action' vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in
cache’
vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in cache' vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in
perform_action’
vendor/rails/actionpack/lib/action_controller/base.rb:524:in send' vendor/rails/actionpack/lib/action_controller/base.rb:524:in
process_without_filters’
vendor/rails/actionpack/lib/action_controller/filters.rb:606:in process_without_session_management_support' vendor/rails/actionpack/lib/action_controller/session_management.rb:134:in
process’
vendor/rails/actionpack/lib/action_controller/base.rb:392:in process' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:184:in
handle_request’
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:112:in dispatch_unlocked' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:125:in
dispatch’
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:124:in synchronize' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:124:in
dispatch’
vendor/rails/actionpack/lib/action_controller/dispatcher.rb:134:in dispatch_cgi' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:41:in
dispatch’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:76:in process' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in
synchronize’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in process' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in
process_client’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in each' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in
process_client’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in run' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
initialize’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in new' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
run’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in initialize' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in
new’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in run' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in
run’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in each' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in
run’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in run' /home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in
run’
/home/chmarus/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281
vendor/rails/activesupport/lib/active_support/dependencies.rb:142:in load_without_new_constant_marking' vendor/rails/activesupport/lib/active_support/dependencies.rb:142:in
load’
vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in new_constants_in' vendor/rails/activesupport/lib/active_support/dependencies.rb:142:in
load’
vendor/rails/railties/lib/commands/servers/mongrel.rb:64
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require' /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in
require’
vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in require' vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in
new_constants_in’
vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in require' vendor/rails/railties/lib/commands/server.rb:49 /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require’
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require’
script/server:3[/quote]
Cóż to może być?