参数错误:添加新中间件后错误的参数数(给定 1,预期为 0)



我对Rails框架没有太多了解。但是我正在关注一个名为Web应用程序开发:Coursera上的基本概念的MOOC。

自举轨道项目几乎处于初始阶段,运行rails serverrake middleware会产生0作为错误代码。

现在我将在根目录中的./lib/timer.rb处创建一个新文件:

class Timer
def initilize(app)
@app = app
end
def call(env)
start = Time.now
status, headers, response = @app.call(env)
stop = Time.now
headers['X-Timing'] = (stop - stop).to_s
[status, headers, response]
end
end

现在,我将让应用程序知道./config/application.rb中有一个新的中间件:

require_relative 'boot'
require 'rails/all'
require_relative '../lib/timer'
Bundler.require(*Rails.groups)
module Railsblog
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
config.middleware.insert_before(Rack::Sendfile, Timer)
end
end

现在rake middleware后:

$ rake middleware --trace
** Invoke middleware (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
ArgumentError: wrong number of arguments (given 1, expected 0)
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/stack.rb:37:in `initialize'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/stack.rb:37:in `new'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/stack.rb:37:in `build'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/stack.rb:131:in `block in build'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/stack.rb:127:in `each'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/stack.rb:127:in `inject'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/stack.rb:127:in `build'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/engine.rb:512:in `block in app'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/engine.rb:508:in `synchronize'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/engine.rb:508:in `app'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/application/finisher.rb:97:in `block in <module:Finisher>'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/initializable.rb:32:in `instance_exec'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/initializable.rb:32:in `run'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/initializable.rb:61:in `block in run_initializers'
/usr/share/ruby/tsort.rb:228:in `block in tsort_each'
/usr/share/ruby/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
/usr/share/ruby/tsort.rb:431:in `each_strongly_connected_component_from'
/usr/share/ruby/tsort.rb:349:in `block in each_strongly_connected_component'
/usr/share/ruby/tsort.rb:347:in `each'
/usr/share/ruby/tsort.rb:347:in `call'
/usr/share/ruby/tsort.rb:347:in `each_strongly_connected_component'
/usr/share/ruby/tsort.rb:226:in `tsort_each'
/usr/share/ruby/tsort.rb:205:in `tsort_each'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/initializable.rb:60:in `run_initializers'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/application.rb:363:in `initialize!'
/home/sntshk/coursera/railsblog/config/environment.rb:5:in `<main>'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.1.10/lib/zeitwerk/kernel.rb:23:in `require'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `block in require'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:291:in `load_dependency'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/application.rb:339:in `require_environment!'
/home/sntshk/coursera/railsblog/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/application.rb:515:in `block in run_tasks_blocks'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:273:in `block in execute'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:273:in `each'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:273:in `execute'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:214:in `block in invoke_with_call_chain'
/usr/share/ruby/monitor.rb:230:in `mon_synchronize'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:194:in `invoke_with_call_chain'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:238:in `block in invoke_prerequisites'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:236:in `each'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:236:in `invoke_prerequisites'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
/usr/share/ruby/monitor.rb:230:in `mon_synchronize'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:194:in `invoke_with_call_chain'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/task.rb:183:in `invoke'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:160:in `invoke_task'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:116:in `each'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:116:in `block in top_level'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:125:in `run_with_threads'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:110:in `top_level'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:83:in `block in run'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:186:in `standard_exception_handling'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/lib/rake/application.rb:80:in `run'
/home/sntshk/.gem/ruby/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
/home/sntshk/bin/rake:23:in `load'
/home/sntshk/bin/rake:23:in `<main>'
Tasks: TOP => middleware => environment

>def initilize(app)看起来像一个错字。应该initialize

相关内容

  • 没有找到相关文章

最新更新