Same URL for different subdomains - ruby-on-rails

I have different views for desktop and mobile site. If site is opened from mobile device then it will be redirected to m.domain_name
root :to => "home#index", constraints: {subdomain: 'm'||'m.staging'}
root :to => 'desktop#index'
It works fine for 'm' subdomain however it isn't working for m.staging subdomain

If you use Request based constraint, the request property should return String, not true/false. Try the same using a lambda,
root :to => "home#index",
constraints: lambda { |request| ['m','m.staging'].include?(request.subdomain) }

Related

Rails Routing Constraint - Limit paths to development

I am trying to add a constraints to our error-routes so that requests to certain routes would not get redirected to a 404-page if we're working on our app in development.
match '*unmatched_route', :to => 'application#handle_404', via: :all, constraints: lambda { |request| !request.url["/rails/info"] }
This is the rule we currently have in place. All "invalid" routes being redirected to a 404-page. Now I want to upgrade this to only allow access to /rails/info/* if the app is in development-mode.
I tried the following:
match '*unmatched_route', :to => 'application#handle_404', via: :all, constraints: lambda { |request| !request.url["/rails/info"] && !Rails.env.development? }
I am not sure where exactly the problem is. The addition inside the constraint does not seem to have any effect, thus I believe I am using the constraint entirely wrong.
Thanks in advance!
Might not be as elegant, but you could put the route inside a unless block, like this:
unless Rails.env.development?
match '*unmatched_route', :to => 'application#handle_404', via: :all, constraints: lambda { |request| !request.url["/rails/info"] }
end

Constraints wildcard subdomain

How can I route all subdomains to a controller in a Rails app?
I though something like this would work, but it doesn't:
constraints :subdomain => '*' do
get '/', :to => 'frontend#index'
mount API => '/api'
end
Subdomains like
foo.example.com
bar.example.com
should be directed to frontend#index while
foo.example.com/api
bar.example.com/api
should call the Grape API.
Use a regular expression:
constraints :subdomain => /./ do...
Haven't tested it, but something like that should work.

Rails: accessing network.domain.com renders root for domain.com

I'm trying to set up a subdomain network for my domain. In my config/routes.rb I have:
constraints :subdomain => /network.*/ do
...
# Root
match '/' => 'questions#index'
match '' => 'questions#index'
root :to => 'questions#index', :as => 'network_root'
end
root :to => 'frontend#home'
However, sometimes when I access network.domain.com (in production) it shows the root for the whole application (frontend#home). Accessing the subdomain in development works as expected.
EDIT: I've just noticed that requests to the app root (domain.com/) and subdomain root (network.domain.com/) do not get logged in my production.log. Any other page loads work as expected.

Rails 3 - Subdomain Issue Moving to Heroku

I've written an application that uses a subdomain per user account to segregate environments. All this is working fine, except I have one issue. I can't get both www and "" to have a different root path than all other subdomains.
For all account subdomains, I have a root page of:
root :to => "applications#index"
I need this to be the root page for all subdomains except for a blank subdomain of "" and then "www". For www, I have this in the routes:
constraints(:subdomain => "www") do
root :to => "promos#index"
end
What I'm struggling with, is getting it so "" will also use promos#index as the root path. When it's not the root path, mywebsite.com sends them to the applications#index, which requires a login. Something I don't want users to see on a first visit.
Is there anyway to modify this code to also include mywebsite.com to have the different root? I've tried things like duplicating the code with "", but this tends to mess up all other subdomains, regardless of order. Below is the in of my routes file:
constraints(:subdomain => "www") do
root :to => "promos#index"
end
root :to => "applications#index"
You can use an object that implements 'matches?' to do some real custom stuff. Below we'll set applications#index if you are a customer subdomain, and send you to promo#index if you're not
In your routes:
Yourapp::Application.routes.draw do
constraints(SubDomain) do
root :to => "applications#index"
end
root :to => "promo#index"
...
end
and then the Subdomain matcher file:
config/initializers/subdomain.rb
class SubDomain
def self.matches?(request)
case request.subdomain
when 'www', '', nil, #admin/api/etc could also go here
false
else
true
end
end
end
subdomain.rb can also live in lib (if it's being auto-loaded)

Ruby on Rails: subdomain too powerful? how do I set this up the right way

routes:
match '/' => 'site_admin/admin#index'
resources :link_pages
resources :services
resource :user_session
resource :account, :controller => "users"
resources :password_resets
resources :users
resources :addresses
resources :info
match "/home", :to => 'info#home'
match "/register", :to => 'users#new'
root :to => 'info#home'
match ':controller(/:action(/:id(.:format)))'
so when I got to admin.lvh.me:3000/ it goes to site_admin/admin#index... which is great...
but when I take off the subdomain, and just have lvh.me:3000/ it goes to the same route....
how do I get admin to stay where it is. and no subdomain to go to my root page, as in my routes file?
Routes are parsed in order, so when you request / from any domain it finds "match '/'..." first and sends you to the specified page. Your subdomain isn't coming into play at all. You can use Request-based constraints to route based on subdomain:
http://guides.rubyonrails.org/routing.html#request-based-constraints
Not sure how subdomain factors into this at all. Perhaps you're confusing subdomain with route namespacing (http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing)?
match '/' => 'site_admin/admin#index'
Is being selected over
root :to => 'info#home'
Because it's defined first in the routes file. They're ostensibly the same thing.
Yes #Cory is right. Above both statements are similar and first defined route is considered every time. If you change admin route to
match '/admin' => 'site_admin/admin#index'
then it does make sense... What say??
or else, using the following code you can determine your URL conditionally:
request.subdomains(0).first will give you the subdomain value- either admin or blank. But it will go to any one controller action only which is defined first in route.rb file.
Then from that action using subdomain, you can decide where to re-direct it- either to admin panel or home page...

Resources