Where to Install Rack Middleware in Rails4.1

I've looked at some of the answers 1] Include in qoutes, 2] Don't use require etc., but none of them work. The solution may have been changed in Rails4

I'm trying to follow the guide here https://devcenter.heroku.com/articles/ruby-websockets#using-with-rails

It says Copy the existing ChatBackend middleware in app / middleware / chat_backend.rb to your Rails project. Then push the middleware to your stack defined in config / application.rb:

require 'chat_backend'
config.middleware.use ChatDemo::ChatBackend

      

I have middleware defined in app / middlewares / chat_backend.rb like this:

require 'faye/websocket'
require 'thread'
require 'redis'
require 'json'
require 'erb'

    module ChatDemo
      class ChatBackend
        KEEPALIVE_TIME = 15 # in seconds
        CHANNEL        = 'twitter-stream'
        def initialize(app)
        end
      /// DELETED CODE for simplicity
    end

      

Here is the application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'chat_backend'  <= ERROR: config/application.rb:4:in `require': cannot load such file -- chat_backend (LoadError)

Bundler.require(*Rails.groups)

module MyProject
  class Application < Rails::Application

    config.middleware.use ChatDemo::ChatBackend
  end
end

      

How can I add middleware. Check out all the accurate code samples.

+3


source to share


3 answers


You don't need to require anything because it app

is in the Rails 4.x download path.

Just put your middleware class in a folder app/middleware

and add it to Rack viaconfig.middleware.use(new_middleware, args)

For example: app/middleware/name_is_important.rb

class NameIsImportant
  # logic goes here
end

      

In application.rb

(string):

config.middleware.use 'NameIsImportant'

      

Or in environment files, for example development.rb

(you can use the class name here):



config.middleware.use NameIsImportant

      

Your specific case doesn't work because you tried to import (which is not needed) a non-existing class. If you want to encapsulate your class in a module for some reason, it must be reflected in the path.

Add your class to the correct directory: app/middleware/chat_demo/chat_backend.rb

module ChatDemo
  class ChatBackend

  end
end

      

In application.rb

:

config.middleware.use 'ChatDemo::ChatBackend'

      

However, I believe this module is unnecessary and this is the root of all your problems with adding middleware to Rack. Try playing with and without a class in a module and with different directories. Pay attention to the output rack middleware

.

+3


source


Typically, Rack middleware is inserted into a Rails application by declaring it internally config/application.rb

. You might need require 'something'

in the header of this file and then inside the application config block config.middleware.use Rack::MiddlewareName

as shown below.

If the middleware comes from a gem, your Gemfile might be require

automatic, so the string require

might be redundant.



require 'bouncy_module'

module Coachpage
  class Application < Rails::Application
    config.i18n.enforce_available_locales = true
    # other config options

    config.middleware.use Rack::Bouncy
  end
end

      

0


source


I ended up including explicitly the filename in application.rb

require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)

module MyProject
  class Application < Rails::Application

    require File.join(Rails.root, 'app/middlewares/chat_backend.rb')
    config.middleware.use ChatDemo::ChatBackend
  end
end

      

-1


source







All Articles