2017-05-17 28 views
4

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> 

これはエラーに述べていますファイル:

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

https://github.com/cyu/rack-cors#rails-configurationを参照してください。コードのどこかに "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 

は、私は、エラーはかなり冗長だと思う

+0

@xSlokあなたがスタックトレースを通して散らばった 'railties-5.1.1 'によって明らかにされるように、あなたはレール5を使用しているので、Rails 5スタイルが必要です – engineersmnky

+1

Thanks everyman 、あなたは私の時間を救った.. –

関連する問題