Accessing request context object in rails router

272 views Asked by At

I need to create a few dynamic routes in my rails router in the following way:

Rails.application.routes.draw do
    account = Account.find_by(
      subdomain: request.subdomain,
      domain: request.domain
    )
    EditableField.where(account_id: account.id).links.each do |link|
      get link.link_href, to: link.method
    end
end

As shown above, I need to determine the account based on the request domain and subdomain, however I cant find how to access the request object in the rails router. What is the correct way to do this ?

1

There are 1 answers

0
Oz Ben-David On

I would suggest using the rack-rewrite gem https://github.com/jtrupiano/rack-rewrite

It is in lower level and give you much more options to do this kind of dynamic routing

And if you still want to do that in the routes.rb file, you can follow this answer https://stackoverflow.com/a/24411835/2529330

Enjoy :)