嗨, 我需要你的cancan帮助。
我有这个控制器:
class RegistrationsController<设计:: RegistrationsController
我需要因为我必须覆盖一些方法。
我需要控制对此控制器的访问权限,因为管理员是唯一可以添加新用户的人。
如何使用load_and_authorize_resource?
我的能力.rb文件是: 班级能力 包括CanCan :: Ability
def initialize(user,controller_namespace) user || = Usuario.new
case controller_namespace
when "Administrator"
if user.rol == 1
can :manage, Faq
can :manage, Tip
can :manage, Administrator::HomeTratamientoController
end
if user.rol == 2
can :manage, :admin_vacunas
can :manage, Registration
end
when "paciente"
if user.rol == 3
can :manage, Paciente::HomeController
end
else
can :read, :all
end
端
该模型是usuario.rb
当rol为1时,我没有问题,但是当时为2,我放了一行:
load_and_authorize_resource
在注册控制器中,我将操作新调用为管理员添加新用户,显示以下错误:
42ms完成500内部服务器错误(ActiveRecord:5.1ms)
NameError - 未初始化的常量注册:
activesupport(4.2.1)lib / active_support / dependencies.rb:533:在load_missing_constant'
activesupport (4.2.1) lib/active_support/dependencies.rb:184:in
const_missing'
activesupport(4.2.1)lib / active_support / inflector / methods.rb:261:in block in constantize'
activesupport (4.2.1) lib/active_support/inflector/methods.rb:259:in
constantize'
activesupport(4.2.1)lib / active_support / core_ext / string / inflections.rb:66:in constantize'
cancancan (2.0.0) lib/cancan/controller_resource.rb:156:in
resource_class'
cancancan(2.0.0)lib / cancan / controller_resource.rb:192:在resource_base'
cancancan (2.0.0) lib/cancan/controller_resource.rb:80:in
build_resource'
cancancan(2.0.0)lib / cancan / controller_resource.rb:61:在load_resource_instance'
cancancan (2.0.0) lib/cancan/controller_resource.rb:35:in
load_resource'
cancancan(2.0.0)lib / cancan / controller_resource.rb:12:在make_lambda中的block in add_before_action'
activesupport (4.2.1) lib/active_support/callbacks.rb:448:in
块中'
activesupport(4.2.1)lib / active_support / callbacks.rb:164:block in halting'
activesupport (4.2.1) lib/active_support/callbacks.rb:504:in
阻止呼叫'
activesupport(4.2.1)lib / active_support / callbacks.rb:504:在call'
activesupport (4.2.1) lib/active_support/callbacks.rb:92:in
_ run_callbacks'
activesupport(4.2.1)lib / active_support / callbacks.rb:776:在_run_process_action_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in
run_callbacks'
actionpack(4.2.1)lib / abstract_controller / callbacks.rb:19:在process_action'
actionpack (4.2.1) lib/action_controller/metal/rescue.rb:29:in
process_action'
actionpack(4.2.1)lib / action_controller / metal / instrumentation.rb:32:在block in process_action'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in
块中的仪器'
activesupport(4.2.1)lib / active_support / notifications / instrumenter.rb:20:in instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in
instrument'
actionpack(4.2.1)lib / action_controller / metal / instrumentation.rb:30:在process_action'
actionpack (4.2.1) lib/action_controller/metal/params_wrapper.rb:250:in
process_action'
activerecord(4.2.1)lib / active_record / railties / controller_runtime.rb:18:in process_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:137:in
进程'
actionview(4.2.1)lib / action_view / rendering.rb:30:在process'
actionpack (4.2.1) lib/action_controller/metal.rb:196:in
发送'
actionpack(4.2.1)lib / action_controller / metal / rack_delegation.rb:13:dispatch'
actionpack (4.2.1) lib/action_controller/metal.rb:237:in
阻止行动'
actionpack(4.2.1)lib / action_dispatch / routing / route_set.rb:74:in dispatch'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:43:in
serve'
actionpack(4.2.1)lib / action_dispatch / routing / mapper.rb:49:in serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:43:in
block in serve'
actionpack(4.2.1)lib / action_dispatch / journey / router.rb:30:在serve'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:819:in
来电'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/rack/agent_hooks.rb:30:in
traced_call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/rack/browser_monitoring.rb:32:in
traced_call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/rack/developer_mode.rb:48:in
traced_call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
warden (1.2.6) lib/warden/manager.rb:35:in
阻止通话'
warden(1.2.6)lib / warden / manager.rb:34:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
来电'
rack(1.6.4)lib / rack / etag.rb:24:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
来电'
rack(1.6.4)lib / rack / conditionalget.rb:25:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
rack(1.6.4)lib / rack / head.rb:13:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
actionpack(4.2.1)lib / action_dispatch / middleware / params_parser.rb:27:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
actionpack(4.2.1)lib / action_dispatch / middleware / flash.rb:260:call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
来电'
rack(1.6.4)lib / rack / session / abstract / id.rb:225:在context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in
电话'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/cookies.rb:560:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
activerecord (4.2.1) lib/active_record/query_cache.rb:36:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
activerecord (4.2.1) lib/active_record/migration.rb:378:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:29:in
阻止通话'
activesupport(4.2.1)lib / active_support / callbacks.rb:88:in _run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in
_ run_call_callbacks'
activesupport(4.2.1)lib / active_support / callbacks.rb:81:in run_callbacks'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/reloader.rb:73:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/remote_ip.rb:78:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in
protected_app_call'
better_errors(2.1.1)lib / better_errors / middleware.rb:79:in better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in
call'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in
阻止通话'
web-console(2.3.0)lib / web_console / middleware.rb:18:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
actionpack(4.2.1)lib / action_dispatch / middleware / show_exceptions.rb:30:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
railties(4.2.1)lib / rails / rack / logger.rb:38:in call_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in
阻止电话'
activesupport(4.2.1)lib / active_support / tagged_logging.rb:68:in block in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in
已标记'
activesupport(4.2.1)lib / active_support / tagged_logging.rb:68:in tagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in
call'
quiet_assets(1.1.0)lib / quiet_assets.rb:27:in call_with_quiet_assets'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
来电'
actionpack(4.2.1)lib / action_dispatch / middleware / request_id.rb:21:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
rack(1.6.4)lib / rack / methodoverride.rb:22:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
rack(1.6.4)lib / rack / runtime.rb:18:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
activesupport(4.2.1)lib / active_support / cache / strategy / local_cache_middleware.rb:28:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
rack(1.6.4)lib / rack / lock.rb:17:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
actionpack(4.2.1)lib / action_dispatch / middleware / static.rb:113:在call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call'
rack(1.6.4)lib / rack / sendfile.rb:113:在call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
来电'
railties(4.2.1)lib / rails / engine.rb:518:call'
railties (4.2.1) lib/rails/application.rb:164:in
来电'
newrelic_rpm(3.16.1.320)lib / new_relic / agent / instrumentation / middleware_tracing.rb:96:in call'
rack (1.6.4) lib/rack/content_length.rb:15:in
call'
thin(1.7.0)lib / thin / connection.rb:86:in block in pre_process'
thin (1.7.0) lib/thin/connection.rb:84:in
pre_process'
thin(1.7.0)lib / thin / connection.rb:53:in process'
thin (1.7.0) lib/thin/connection.rb:39:in
receive_data'
eventmachine(1.2.0.1)lib / eventmachine.rb:194:在run'
thin (1.7.0) lib/thin/backends/base.rb:73:in
开始'
thin(1.7.0)lib / thin / server.rb:162:in start'
rack (1.6.4) lib/rack/handler/thin.rb:19:in
run'
rack(1.6.4)lib / rack / server.rb:286:in start'
railties (4.2.1) lib/rails/commands/server.rb:80:in
start'
railties(4.2.1)lib / rails / commands / commands_tasks.rb:80:in block in server'
railties (4.2.1) lib/rails/commands/commands_tasks.rb:75:in
server'
railties(4.2.1)lib / rails / commands / commands_tasks.rb:39:in run_command!'
railties (4.2.1) lib/rails/commands.rb:17:in
'
bin / rails:9:<top (required)>'
spring (1.7.2) lib/spring/client/rails.rb:28:in
来电&#39;
spring(1.7.2)lib / spring / client / command.rb:7:in call'
spring (1.7.2) lib/spring/client.rb:30:in
run&#39;
弹簧(1.7.2)箱/弹簧:49:在<top (required)>'
spring (1.7.2) lib/spring/binstub.rb:11:in
&#39;
bin / spring:13:<top (required)>'
bin/rails:3:in
&#39;
有些人可以帮助我吗?
答案 0 :(得分:0)
您撰写了can :manage, Registration
,因此您需要Registration
模型。
在您的情况下,您应该将其替换为:
can :create, User
并在控制器中load_and_authorize_resource :user, parent: false
https://github.com/CanCanCommunity/cancancan/wiki/authorizing-controller-actions
免费提示:
不要用西班牙语写出模型名称,而是用英语写总是。因此,请将Usuario
替换为User
。