2017-05-17 169 views
4

我是Ruby on Rails的新手,正在學習如何使用Angular,但在運行「gem install rack-cors」之後,當我嘗試啓動rails應用程序時,我不斷收到錯誤:運行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> 

這是對錯誤中提到的文件:

require "active_support/inflector/methods" 
require "active_support/dependencies" 

module ActionDispatch 
    class MiddlewareStack 
    class Middleware 
     attr_reader :args, :block, :klass 

     def initialize(klass, args, block) 
     @klass = klass 
     @args = args 
     @block = block 
     end 

     def name; klass.name; end 

     def ==(middleware) 
     case middleware 
     when Middleware 
      klass == middleware.klass 
     when Class 
      klass == middleware 
     end 
     end 

     def inspect 
     if klass.is_a?(Class) 
      klass.to_s 
     else 
      klass.class.to_s 
     end 
     end 

     def build(app) 
     klass.new(app, *args, &block) 
     end 
    end 

    include Enumerable 

    attr_accessor :middlewares 

    def initialize(*args) 
     @middlewares = [] 
     yield(self) if block_given? 
    end 

    def each 
     @middlewares.each { |x| yield x } 
    end 

    def size 
     middlewares.size 
    end 

    def last 
     middlewares.last 
    end 

    def [](i) 
     middlewares[i] 
    end 

    def unshift(klass, *args, &block) 
     middlewares.unshift(build_middleware(klass, args, block)) 
    end 

    def initialize_copy(other) 
     self.middlewares = other.middlewares.dup 
    end 

    def insert(index, klass, *args, &block) 
     index = assert_index(index, :before) 
     middlewares.insert(index, build_middleware(klass, args, block)) 
    end 

    alias_method :insert_before, :insert 

    def insert_after(index, *args, &block) 
     index = assert_index(index, :after) 
     insert(index + 1, *args, &block) 
    end 

    def swap(target, *args, &block) 
     index = assert_index(target, :before) 
     insert(index, *args, &block) 
     middlewares.delete_at(index + 1) 
    end 

    def delete(target) 
     middlewares.delete_if { |m| m.klass == target } 
    end 

    def use(klass, *args, &block) 
     middlewares.push(build_middleware(klass, args, block)) 
    end 

    def build(app = Proc.new) 
     middlewares.freeze.reverse.inject(app) { |a, e| e.build(a) } 
    end 

    private 

     def assert_index(index, where) 
     i = index.is_a?(Integer) ? index : middlewares.index { |m| m.klass == index } 
     raise "No such middleware to insert #{where}: #{index.inspect}" unless i 
     i 
     end 

     def build_middleware(klass, args, block) 
     Middleware.new(klass, args, block) 
     end 
    end 
end 

如何解決這個問題呢?

+0

我認爲錯誤是相當冗長。你的代碼中有一個字符串「Rack :: Cors」。你只需要擺脫引號。它會是一個類,可以響應'新'方法 –

回答

18

使用Rails 5時,請刪除引號。

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 

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

    end 
end 

https://github.com/cyu/rack-cors#rails-configuration

+0

@xSlok你需要Rails 5風格,因爲你正在使用Rails 5,就像'railties-5.1.1'通過你的堆棧跟蹤 – engineersmnky

+1

散佈一樣明顯感謝everyman ,你節省了我的時間.. –

相關問題