How to fix undefined method `new' for "Rack::Cors":String
Asked Answered
S

1

18

I'm new to Ruby on Rails and am learning to use Angular with it, but after I ran "gem install rack-cors", when I try to start a Rails application, I keep getting this error:

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>

This is the file that's mentioned on the error:

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

How do I solve it?

Spermic answered 17/5, 2017 at 3:55 Comment(1)
I think the error îs pretty verbose. You have somewhere in your code the string "Rack::Cors". You just have to get rid of the quotes. And it will be a class that can respond to the 'new' methodCorissa
S
73

When using Rails 5 or Rails 6, remove the quotes.

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

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

Study answered 17/5, 2017 at 5:21 Comment(2)
@Spermic you need the Rails 5 style since you are using rails 5 as is evident by railties-5.1.1 littered through out your stack traceKagoshima
thanks. Little note, that is needed from 5.1.0, 5.0.0 still works with the 3/4.x variant - if of any interestProw

© 2022 - 2024 — McMap. All rights reserved.