我正在尝试使用 url_for(:back) 在用户登录时创建一个重定向到上一页的重定向。
当用户自己进入登录页面时,我已经成功地工作了。但是,当用户由于访问需要对用户进行身份验证的页面而被重定向到登录页面时,重定向会将用户返回到他/她尝试访问权限不足的页面之前的页面。
我正在尝试修改我的登录控制器操作以正确处理重定向。我的计划是在发生强制重定向时使用查询字符串参数“redirect”。在控制器中,如果该参数存在,则使用该 URL;否则,使用 url_for(:back),或者如果这不起作用(由于缺少 HTTP_REFERER),那么用户将被重定向到站点的主页。
这是应该实现此逻辑的代码片段:
if params[:redirect]
@url = params[:redirect]
else
@url = url_for :back
@url ||= url_for :controller => "home", :action => "index"
end
我得到的错误是:
用户控制器中的 NoMethodError#login 未定义的方法 `back_url' 用于# RAILS_ROOT:[模糊] 应用程序跟踪 | 框架跟踪 | 全跟踪 供应商/rails/actionpack/lib/action_controller/polymorphic_routes.rb:112:in `__send__' 供应商/rails/actionpack/lib/action_controller/polymorphic_routes.rb:112:in `polymorphic_url' 供应商/rails/actionpack/lib/action_controller/base.rb:628:in `url_for' app/controllers/users_controller.rb:16:in `login' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:76:in `进程' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in `同步' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:在“进程”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `每个' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:在“运行”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `初始化' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:在“运行”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:在“运行”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in `run' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `each' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:在“运行”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in `run' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in `run' /var/lib/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281 供应商/rails/actionpack/lib/action_controller/polymorphic_routes.rb:112:in `__send__' 供应商/rails/actionpack/lib/action_controller/polymorphic_routes.rb:112:in `polymorphic_url' 供应商/rails/actionpack/lib/action_controller/base.rb:628:in `url_for' 供应商/rails/actionpack/lib/action_controller/base.rb:1256:in `send' 供应商/rails/actionpack/lib/action_controller/base.rb:1256:in `perform_action_without_filters' 供应商/rails/actionpack/lib/action_controller/filters.rb:617:in `call_filters' 供应商/rails/actionpack/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark' 供应商/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in `measure' 供应商/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' 供应商/rails/actionpack/lib/action_controller/rescue.rb:136:in `perform_action_without_caching' 供应商/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in `perform_action' 供应商/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache' 供应商/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache' 供应商/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in `perform_action' 供应商/rails/actionpack/lib/action_controller/base.rb:524:in `send' 供应商/rails/actionpack/lib/action_controller/base.rb:524:in `process_without_filters' 供应商/rails/actionpack/lib/action_controller/filters.rb:606:in `process_without_session_management_support' 供应商/rails/actionpack/lib/action_controller/session_management.rb:134:in `process' 供应商/rails/actionpack/lib/action_controller/base.rb:392:在“进程”中 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:184:in `handle_request' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:112:in `dispatch_unlocked' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:125:in `dispatch' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:124:在“同步”中 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:124:in `dispatch' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:134:in `dispatch_cgi' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:41:in `dispatch' 供应商/rails/activesupport/lib/active_support/dependencies.rb:142:in `load_without_new_constant_marking' 供应商/rails/activesupport/lib/active_support/dependencies.rb:142:in `load' 供应商/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in' 供应商/rails/activesupport/lib/active_support/dependencies.rb:142:in `load' 供应商/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' 供应商/rails/activesupport/lib/active_support/dependencies.rb:153:in `require' 供应商/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in' 供应商/rails/activesupport/lib/active_support/dependencies.rb:153:in `require' 供应商/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' 脚本/服务器:3 供应商/rails/actionpack/lib/action_controller/polymorphic_routes.rb:112:in `__send__' 供应商/rails/actionpack/lib/action_controller/polymorphic_routes.rb:112:in `polymorphic_url' 供应商/rails/actionpack/lib/action_controller/base.rb:628:in `url_for' app/controllers/users_controller.rb:16:in `login' 供应商/rails/actionpack/lib/action_controller/base.rb:1256:in `send' 供应商/rails/actionpack/lib/action_controller/base.rb:1256:in `perform_action_without_filters' 供应商/rails/actionpack/lib/action_controller/filters.rb:617:in `call_filters' 供应商/rails/actionpack/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark' 供应商/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in `measure' 供应商/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue' 供应商/rails/actionpack/lib/action_controller/rescue.rb:136:in `perform_action_without_caching' 供应商/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in `perform_action' 供应商/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache' 供应商/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache' 供应商/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in `perform_action' 供应商/rails/actionpack/lib/action_controller/base.rb:524:in `send' 供应商/rails/actionpack/lib/action_controller/base.rb:524:in `process_without_filters' 供应商/rails/actionpack/lib/action_controller/filters.rb:606:in `process_without_session_management_support' 供应商/rails/actionpack/lib/action_controller/session_management.rb:134:in `process' 供应商/rails/actionpack/lib/action_controller/base.rb:392:在“进程”中 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:184:in `handle_request' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:112:in `dispatch_unlocked' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:125:in `dispatch' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:124:在“同步”中 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:124:in `dispatch' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:134:in `dispatch_cgi' 供应商/rails/actionpack/lib/action_controller/dispatcher.rb:41:in `dispatch' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:76:in `进程' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in `同步' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:在“进程”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `每个' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:在“运行”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `初始化' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:在“运行”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:在“运行”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in `run' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `each' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:在“运行”中 /var/lib/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in `run' /var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in `run' /var/lib/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281 供应商/rails/activesupport/lib/active_support/dependencies.rb:142:in `load_without_new_constant_marking' 供应商/rails/activesupport/lib/active_support/dependencies.rb:142:in `load' 供应商/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in' 供应商/rails/activesupport/lib/active_support/dependencies.rb:142:in `load' 供应商/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' 供应商/rails/activesupport/lib/active_support/dependencies.rb:153:in `require' 供应商/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in' 供应商/rails/activesupport/lib/active_support/dependencies.rb:153:in `require' 供应商/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' 脚本/服务器:3 要求 参数: 没有任何 显示会话转储 --- :用户: :csrf_id: 2927cca61bbbe97218362b5bcdb74c0f flash: !map:ActionController::Flash::FlashHash {} 回复 标题: {"内容类型"=>"", “饼干”=>[], “缓存控制”=>“无缓存”}
请记住,我之前已经让它工作了—— url_for(:back) 在我添加这个逻辑之前就知道如何正确操作。
提前感谢您的任何想法!