简体   繁体   English

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

[英]ArgumentError: wrong number of arguments (given 1, expected 0) after adding a new middleware

I don't have much understanding of the Rails framework.我对 Rails 框架了解不多。 But I'm following a MOOC called Web Application Development: Basic Concepts on Coursera.但我正在关注一个名为Web 应用程序开发:Coursera 上的基本概念的 MOOC。

The bootstrapped rails project is almost at the initial stage, and running rails server and rake middleware yields 0 as error code.引导式 rails 项目几乎处于初始阶段,运行rails serverrake middleware会产生0作为错误代码。

Now I'll create a new file at ./lib/timer.rb in root directory:现在我将在根目录的./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

Now, I'll let the application know there's a new middleware in ./config/application.rb :现在,我会让应用程序知道./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

Now after rake middleware :现在在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) looks like a typo. def initilize(app)看起来像一个错字。 Should be initialize应该initialize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FactoryGirl ArgumentError:参数数量错误(给定1,预期为0) - FactoryGirl ArgumentError: wrong number of arguments (given 1, expected 0) ArgumentError:参数数量错误(给定0,应为1..2) - ArgumentError: wrong number of arguments (given 0, expected 1..2) ArgumentError(参数数量错误(给定 1,预期为 2)) - ArgumentError (wrong number of arguments (given 1, expected 2)) Rails 中的 ArgumentError(arguments 的编号错误(给定 5,预期为 1)) - ArgumentError (wrong number of arguments (given 5, expected 1)) in Rails Rails-ArgumentError(参数数量错误(给定1,预期为0)): - Rails - ArgumentError (wrong number of arguments (given 1, expected 0)): Rails-ArgumentError给定的参数个数错误3,预期为2 - Rails - ArgumentError wrong number of arguments given 3, expected 2 ArgumentError(arguments 的编号错误(给定 2,预期 0..1) - ArgumentError (wrong number of arguments (given 2, expected 0..1) ArgumentError错误的参数数量(给定0,应为1)-Scss文件 - ArgumentError wrong number of arguments (given 0, expected 1) - Scss files UsersController#中的ArgumentError创建错误的参数数量(给定0,应为1) - ArgumentError in UsersController#create wrong number of arguments (given 0, expected 1) Rails 中止了! ArgumentError:参数数量错误(给定 0,预期为 1..2) - Rails aborted! ArgumentError: wrong number of arguments (given 0, expected 1..2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM