Rails: accessing network.domain.com renders root for domain.com - ruby-on-rails

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.

Related

Same URL for different subdomains

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) }

How can I redirect multiple devise model to multiple specific urls?

I got three independent devise models, ergo I got three different sign_in screen. And all three have got a dashboard:
devise_for :md1
devise_for :md2
devise_for :md3
match 'md1/dashboard' => 'md1#dashboard', :via => :get
match 'md2/dashboard' => 'md2#dashboard', :via => :get
match 'md3/dashboard' => 'md3#dashboard', :via => :get
I want when there is a mdX succesfully sign in, it will redirect to mdX#dashboard, and if it is possible by GET. I tried:
devise_scope :md1 do
root :to => 'md1#dashboard'
end
devise_scope :md2 do
root :to => 'md2#dashboard'
end
devise_scope :md3 do
root :to => 'md3#dashboard'
end
Then when I succesfully sign in with md1 I got redirected to md1 dashboard, but when I succesfully sign in with md2 I got redirected to md1's sign_in screen.
Then I tried:
def after_sign_in_path_for resource
dashboard_path resource
end
But there isn't such method. Is there an easy way to do this or it has to be with the if statements for each model?
UPDATE
Some routes to make a better understanding and more information to get a better solution
md1_dashboard GET /md1/dashboard(.:format) md1#dashboard
md2_dashboard GET /md2/dashboard(.:format) md2#dashboard
md3_dashboard GET /md3/dashboard(.:format) md3#dashboard
Thanks in advance
When you are writing this:
devise_scope :md1 do
root :to => 'md1#dashboard'
end
devise_scope :md2 do
root :to => 'md2#dashboard'
end
devise_scope :md3 do
root :to => 'md3#dashboard'
end
You are defining three root routes, with the same name. Since they conflict, only the first will be used. That's why only md1 worked. You probably meant to write this:
scope :md1 do
root :to => 'md1#dashboard'
end
scope :md2 do
root :to => 'md2#dashboard'
end
scope :md3 do
root :to => 'md3#dashboard'
end
On this case, you will define three different root routes, at three different scopes (check rake routes again). Note scope is a router method that scopes your routes, devise_scope does not scope any route, it simply tells which devise scope you want to use, which you don't need to tell unless Devise explicitly asks you so (you will know when it does).
After this change, everything should work as expected. Note that Devise by default uses #{scope}_root_path to redirect after successful sign in, that's why the code above works (check rake routes and you will see you md1_root, md2_root, etc are now defined).

RESTful routing in Rails 3 app - some advice / pointers

I have an app, locally in development without a subfolder, and in production i have it deployed under /myappname/
So, locally I have http://myapp.dev and in production http://mydomain.com/myappname
which my root route does:
root :to => 'products#list'
which works great, even in production.
Now, i have a default match action:
match '/:controller(/:action(/:id))'
which breaks in production, so i started trying to build a restful route, but i need some help... I can't wrap my head around the routing. I think i have the proper start (with scope, below)
#PRODUCTION ROUTES
scope '/myappname' do
#WHAT WOULD GO HERE?
end
format would be /myappname/products/show/15
Hm. I would expect all the routes to work relative to the "home page", so why don't you just go with
resources :users
or any other route definition from the examples in config/routes.rb?
#PRODUCTION ROUTES
scope "/mothers" do
#ROOT
root :to => 'rings#list'
match '/rings/:id' => "rings#show", :as => :ring
end
#DEVELOPMENT ROUTES
root :to => 'rings#list'
match '/rings/:id' => "rings#show", :as => :ring

subdomain for static page in rails 3

I have a Rails 3 app that needs to have subdomains that point to a static page in my code base
EXAMPLE:
mk1.mysite.com needs to show the page that is located
app > views > about > page.html.haml
That page sits at mysite.com/about/page.html
QUESTION:
How can I configure the routes to display the subdirectory (mysite.com/about/page.html) page by visiting the subdomain (mk1.mysite.com)?
I have this in my routes.rb
match '/' => 'about/page.html', :constraints => { :subdomain => 'mk1' }
Where is your regular root route located in your routes file? Is it located before or after the line you listed in your post?
So if you have a line like
root :to => "foo#index"
In your routes file, above
match '/' => 'about/page.html', :constraints => { :subdomain => 'mk1' }
The root route would be called first and your subdomain route won't be called.

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)

Resources