Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何修复未定义的方法“new';至于;机架::Cors";:一串_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 如何修复未定义的方法“new';至于;机架::Cors";:一串

Ruby on rails 如何修复未定义的方法“new';至于;机架::Cors";:一串,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我是Ruby on Rails的新手,正在学习如何使用Angular,但在我运行“gem install rack cors”之后,当我尝试启动Rails应用程序时,我不断遇到以下错误: C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:35:in `build': undefined method `new' for "Rack::Cors":Strin

我是Ruby on Rails的新手,正在学习如何使用Angular,但在我运行“
gem install rack cors
”之后,当我尝试启动Rails应用程序时,我不断遇到以下错误:

C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:35:in `build': undefined method `new' for "Rack::Cors":String (NoMethodError)
Did you mean?  next
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:99:in `block in build'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:99:in `each'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:99:in `inject'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:99:in `build'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/engine.rb:508:in `block in app'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/engine.rb:504:in `synchronize'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/engine.rb:504:in `app'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/application/finisher.rb:45:in `block in <module:Finisher>'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/initializable.rb:30:in `instance_exec'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/initializable.rb:30:in `run'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/initializable.rb:59:in `block in run_initializers'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `each'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `call'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
        from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/initializable.rb:58:in `run_initializers'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/application.rb:353:in `initialize!'
        from D:/TFE Unipar/Vigia Back-End/config/environment.rb:5:in `<top (required)>'
        from config.ru:3:in `require_relative'
        from config.ru:3:in `block in <main>'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:55:in `instance_eval'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:55:in `initialize'
        from config.ru:in `new'
        from config.ru:in `<main>'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:49:in `eval'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:49:in `new_from_string'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:40:in `parse_file'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/server.rb:319:in `build_app_and_options_from_config'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/server.rb:219:in `app'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:24:in `app'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/server.rb:354:in `wrapped_app'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:80:in `log_to_stdout'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:42:in `start'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:131:in `block in perform'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:126:in `tap'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:126:in `perform'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/command/base.rb:63:in `perform'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/command.rb:44:in `invoke'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands.rb:16:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>

如何解决此问题?

使用Rails 5或Rails 6时,请删除引号

module YourApp
  class Application < Rails::Application

    # ...

    # Rails 3/4

    config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end

    # Rails 5/6

    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end

  end
end
moduleyourapp
类应用程序:any,:methods=>[:get,:post,:options]
结束
结束
#轨道5/6
config.middleware.insert_在0之前,Rack::Cors do
允许做
起源“*”
资源“*”,:headers=>:any,:methods=>[:get,:post,:options]
结束
结束
结束
结束

请看

我认为这个错误相当冗长。您的代码中有字符串“Rack::Cors”。你只要去掉引号就行了。这将是一个能够对“新”作出反应的班级method@xSlok您需要Rails 5样式,因为您使用的是Rails 5,这一点在堆栈跟踪中随处可见。值得注意的是,5.1.0中需要用到这一点,5.0.0仍然适用于3/4.x变体——如果有兴趣的话
module YourApp
  class Application < Rails::Application

    # ...

    # Rails 3/4

    config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end

    # Rails 5/6

    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end

  end
end