Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter data in ruby on rails by subdomain

I have 2 tables news and news_type. Also I have 3 base types for news: politic, technology and sport. How I can filter this data by subdomain?

For example:

If I go to example.com I get all news in my home page

If I go to sport.example.com I get all news with type sport

And so on.

like image 410
itdxer Avatar asked Feb 10 '26 06:02

itdxer


1 Answers

In your routes.rb file, you can provide constraints on the path. For example:

# routes.rb
...
get "/" => "sports#index",  constraints: { domain: "sport.example.com" }
get "/" => "tech#index",  constraints: { domain: "tech.example.com" }
root :to => 'static#index'

This would route

  • sport.example.com to the index action within SportsController
  • tech.example.com to the index action within TechsController
  • example.com to the index action within StaticController
like image 53
Tyler Avatar answered Feb 16 '26 17:02

Tyler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!