简体   繁体   中英

Passing multiple arguments to Rack middleware in Rails application.rb

I am creating a Rack Middleware which I want to use in my Rails App. Basically, I need to log requests matching particular urls to my Database. In order to do this I need to pass database configuration to my Middleware so it can establish connection with DB. I am trying to do:-

db_yml = Rails.root.join('config/database.yml')
db_config = YAML.load(db_yml.read)[Rails.env]

But this is giving an error

config/application.rb:40:in <class:Application>': undefined method read' for # (NoMethodError)

If I add byebug and run the same in the byebug console it works fine. I am not able to find out why. I want to do following things:-

  1. I need to read & modify the database configuration before passing it to my middleware as argument.
  2. I want to read the domain of the request url. We are using Apartment gem and Our schema name will be the domain name.

I followed multiple articles here & here . I am a newbie to Rails and don't know good resources so please help. Thanks in advance!

You should use:

db_yml = Rails.root.join('config/database.yml')
db_config = YAML.load(File.open(db_yml))[Rails.env]

Rails.root.join('config/database.yml') return the file path which is a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM