当我刷新页面时,Rails重新渲染,并使用搞定的参数

时间:2017-08-24 20:59:35

标签: ruby-on-rails ruby

我有一个名为bulk_edit的页面。它有一条路线:

   post :bulk_edit

在我的routes.rb。

视图本身属于HAML并且可以正常运行。我的控制器中还有一个名为bulk_edit的方法:

def bulk_edit
  print_method
  assets = params[:assets] || ''
  @assets = VodAsset.where(id: assets.split(','))
end

VodAsset是我的模特之一。问题是,当我到达我的页面然后点击刷新时,发生了混乱 - 我得到了这个:

Bizarre error... never do I pass this

我的show方法是这样的:

def show
    print_method
    @options = VodAsset.all.map { |a| [a.id, a.title_asset_id] }.to_h
end

据我所知,非常无害。 id是我的模型(因为它适用于所有模型)的一个键,称为VodAsset。我无法理解在任何场景,刷新或其他方面,我们最终都尝试使用id-> bulk_edit进行渲染?不知怎的,方法名称进入实际调用,这是没有意义的,特别是我现在非常简单的设置。

我正在点击刷新页面......想知道,我将在哪里开始解决这个问题?似乎没有足够的保证这个错误。 ole red页面上的堆栈跟踪没有一行与我的控制器代码或我项目中的任何代码有关,因此它不是一个非常好的起点。

这是整个堆栈跟踪 - 但我无法做任何事情,因为它没有关于我编写的文件中的任何代码的任何内容:

activerecord (4.2.3) lib/active_record/core.rb:155:in `find'
cancancan (2.0.0) lib/cancan/model_adapters/abstract_adapter.rb:20:in `find'
cancancan (2.0.0) lib/cancan/controller_resource.rb:104:in `find_resource'
cancancan (2.0.0) lib/cancan/controller_resource.rb:63:in `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:28:in `load_and_authorize_resource'
cancancan (2.0.0) lib/cancan/controller_resource.rb:12:in `block in add_before_action'
activesupport (4.2.3) lib/active_support/callbacks.rb:446:in `instance_exec'
activesupport (4.2.3) lib/active_support/callbacks.rb:446:in `block in make_lambda'
activesupport (4.2.3) lib/active_support/callbacks.rb:162:in `block in halting'
activesupport (4.2.3) lib/active_support/callbacks.rb:502:in `block in call'
activesupport (4.2.3) lib/active_support/callbacks.rb:502:in `each'
activesupport (4.2.3) lib/active_support/callbacks.rb:502:in `call'
activesupport (4.2.3) lib/active_support/callbacks.rb:88:in `run_callbacks'
actionpack (4.2.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.3) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.3) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.3) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.3) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.3) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.3) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.3) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.3) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.3) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:821:in `call'
bullet (5.6.0) lib/bullet/rack.rb:10:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (1.6.8) lib/rack/etag.rb:24:in `call'
rack (1.6.8) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.8) lib/rack/head.rb:13:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.8) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.8) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.3) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.3) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.3) lib/active_support/callbacks.rb:84:in `run_callbacks'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.8) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.8) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.8) lib/rack/lock.rb:17:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.8) lib/rack/sendfile.rb:113:in `call'
railties (4.2.3) lib/rails/engine.rb:518:in `call'
railties (4.2.3) lib/rails/application.rb:165:in `call'
rack (1.6.8) lib/rack/content_length.rb:15:in `call'
unicorn (5.3.0) lib/unicorn/http_server.rb:606:in `process_client'
unicorn (5.3.0) lib/unicorn/http_server.rb:702:in `worker_loop'
unicorn (5.3.0) lib/unicorn/http_server.rb:549:in `spawn_missing_workers'
unicorn (5.3.0) lib/unicorn/http_server.rb:142:in `start'
unicorn-rails (2.2.1) lib/unicorn_rails.rb:33:in `run'
rack (1.6.8) lib/rack/server.rb:287:in `start'
railties (4.2.3) lib/rails/commands/server.rb:80:in `start'
railties (4.2.3) lib/rails/commands/commands_tasks.rb:80:in `block in server'
railties (4.2.3) lib/rails/commands/commands_tasks.rb:75:in `tap'
railties (4.2.3) lib/rails/commands/commands_tasks.rb:75:in `server'
railties (4.2.3) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
railties (4.2.3) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:8:in `require'
bin/rails:8:in `<top (required)>'
spring (2.0.2) lib/spring/client/rails.rb:28:in `load'
spring (2.0.2) lib/spring/client/rails.rb:28:in `call'
spring (2.0.2) lib/spring/client/command.rb:7:in `call'
spring (2.0.2) lib/spring/client.rb:30:in `run'
spring (2.0.2) bin/spring:49:in `<top (required)>'
spring (2.0.2) lib/spring/binstub.rb:31:in `load'
spring (2.0.2) lib/spring/binstub.rb:31:in `<top (required)>'
/Users/philjohnson/.rbenv/versions/2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
/Users/philjohnson/.rbenv/versions/2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
bin/spring:13:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

1 个答案:

答案 0 :(得分:3)

我将假设一个更完整的路由文件示例如下:

resources :vod_assets do
  collection do
    post :bulk_edit
  end
end

这将导致VOD资产的一系列路线,我们感兴趣的2是:

              Prefix Verb URI Pattern                     Controller#Action
bulk_edit_vod_assets POST /vod_assets/bulk_edit(.:format) vod_assets#bulk_edit
           vod_asset GET  /vod_assets/:id(.:format)       vod_assets#show

当我们以这种方式查看路线时,我们可以看到一个是POST,一个是GET。如果您尝试GET/vod_assets/bulk_edit 将<{1}}路由匹配,但匹配{ {1}} POST /vod_assets/bulk_editGET /vod_assets/:id。然后,这会将您发送到您的控制器上的id操作,但是您有一个"bulk_edit"某处(看起来像show相关的东西)尝试使用{加载before_action {1}}现在是cancan

因此,当您刷新页面时会发生这种情况,因为浏览器正在发送VodAsset而不是params[:id]并且您没有达到预期的路线,因为动词不会匹配。

如果您愿意,可以在路线上设置一些限制条件,以阻止"bulk_edit"进入GET路线,这样的事情就应该这样做(未经测试):

POST

现在刷新时,您应该收到"bulk_edit"错误而不是当前错误。