How to setup specific subdomain for rails routes? - ruby-on-rails

I am trying to setup a subdomain for my site in rails. I followed to rails cast to do this.
http://railscasts.com/episodes/221-subdomains-in-rails-3?view=comments
I am having an issue. I want to state that for a specific static subdomain as a secondary homepage.
constraints(Subdomain) do
match '/' => 'static_pages#secondary_home'
end
root to: 'static_pages#home'
lib/subdomain.rb
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain.eql? "secondaryhome"
end
end
so now I expect only secondaryhome.lvh.me:3000 to point to my secondary homepage. (lvh.me is and external domain that points to localhost) .However, any subdomain I have (for instance abc.lvh.me:3000) seems to be pointing to the secondary home, while i want it to default to my primary root. What should i do?

We've done the equivalent of this:
#config/routes.rb
constraints({ subdomain: "secondaryhome" }) do
match '/' => 'static_pages#secondary_home'
end
This sets a route for lvh.me:3000 with constraint { subdomain: "secondaryhome" } - only that should work

Related

allow access to one page on sub-domain, and if tried accessing other pages redirect to main domain

allow access to one page(campaigns/test) on sub-domain(aap.example.com), and if tried accessing other pages redirect to main domain(example.com)
app.example.com is my sub-domain of my main domain example.com, so same rails app is running on domain and sub-domain.
I tried some solutions like adding constraints in routes file and nginx file as well, but not getting what I need.
routes.rb
constraints(Subdomain) do
match '/campaigns/:slug' => 'campaigns#show', via: [:get]
match "/cam_enquiry" => "campaigns#cam_enquiry", via: [:post]
end
and the subdomain.rb module
class Subdomain
def self.matches?(request)
case request.subdomain
when 'app', '', nil
false
else
true
end
end
end
Let me know if you need anymore details.
Define all your routes and after it define constraint for app subdomain. You need to match test route and use redirect for all other subdomain routes. Specify only blank subdomain in redirect parameters, in this case it will get all other info from request and only change subdomain to main domain
get 'campaigns/test', to: redirect(subdomain: 'app'), constraints: { subdomain: '' }
get '/campaigns/:slug', to: 'campaigns#show'
post '/cam_enquiry', to: 'campaigns#cam_enquiry'
constraints subdomain: 'app' do
get 'campaigns/test', to: 'contoroller#action'
get '/*any', to: redirect(subdomain: '')
end

Separate Domain for Namespaced Routes in Rails 4

I'm working on a Rails 4 app. One part of the app is a customer portal that has to be accessed from a separate domain.
I have everything working fine by navigating to domain.com/cp. The routes use namespaced controllers:
namespace :cp do
get :dashboard, to: 'dashboard#index', path: ''
...
end
How should I set up DNS records and change the routes definition so that another domain cpdomain.com points to domain.com/cp properly (no redirecting).
Thanks.
This answer can be useful for the rails routes problem:
Rails routing to handle multiple domains on single application
Shortened:
1) define a custom constraint class in lib/domain_constraint.rb:
class DomainConstraint
def initialize(domain)
#domains = [domain].flatten
end
def matches?(request)
#domains.include? request.domain
end
end
2) use the class in your routes with the new block syntax
constraints DomainConstraint.new('mydomain.com') do
root :to => 'mydomain#index'
end
root :to => 'main#index'
or the old-fashioned option syntax
root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')

Rails namespace admin on custom subdomain

My rails app is set to use subdomains as described in this RailsCast:
http://railscasts.com/episodes/221-subdomains-in-rails-3
Now, I would like to add an admin subdomain to the front of my blog subdomain as follows:
admin.company.lvh.me:3000
I've tried to namespace admin outside of my Subdomain constraint:
namespace :admin, path: '/', constraints: { subdomain: 'admin' } do
constraints(Subdomain) do
match '/', to: 'blogs#show', via: 'get'
end
end
But instead of routing through my app/controllers/admin/blogs_controller it attempts to route through my "normal user" controller (app/controllers/blogs_controller).
Am I just missing something simple or is doing something like this in rails much more difficult?
I was able to solve this, although it feels a little hackish. Understanding that Rails treats constraints either true or false, I set another constraint inside the initial subdomain constraint check. It splits the subdomain in 2 and examines the first subdomain to see if it equals "admin". If true, it routes to the admin/controllers and admin/views (because of module: "admin"), if not, it routes to the less specific routes that are not inside the "admin" module.
At first I didn't have the namespace :admin, and my route helpers were incorrect (the admin routes weren't prefixed with "admin" and the less specific routes weren't being set since they were duplicates). Once I added namespace :admin and the path: "" (this is important, too, because it removes "admin/" from the URI Pattern), it worked!
One last thing, in the admin/controllers, you have to edit the set_blog method, since "admin.company" is being interpreted instead (see admin/blogs_controller.rb).
routes.rb
Blog::Application.routes.draw do
constraints(Subdomain) do
namespace :admin, module: "admin", path: "", constraints: lamda { |r| r.subdomain.split('.')[0] == 'admin' } do
match '/', to: 'blogs#show', via: 'get'
...
end
match '/', to: 'blogs#show', via: 'get'
...
end
...
end
Rake Routes:
Prefix Verb URI Pattern Controller#Action
admin GET / admin/blogs#show
...
​ GET / blogs#show
...
admin/blogs_controller.rb
BlogController < ApplicationController
before_action :set_blog
...
private
set_blog
#blog = Blog.find_by_subdomain!(request.subdomain.split('.')[1])
end
end
Let me know if there's anything cleaner out there, if not, hopefully this helps others with this issue.
There are several important factors here
Firstly, you'll need to see what the constraint params look like with "multi" subdomains. Instead of splitting, Rails may have admin.company as the subdomain
If we take the idea that Rails will split the subdomains into two, which one is being called as the "parent"?
namespace :admin, path: '/', constraints: { subdomain: 'admin' } do
constraints(Subdomain) do
resources :blogs, only: :show, path_names: { show: "" }
end
end
If you give us some more info on the request (params etc), we'll be in a much better position to help!

Ruby on Rails route for wildcard subdomains to a controller/action

I dynamically create URLs of the form username.users.example.com:
bob.users.example.com
tim.users.example.com
scott.users.example.com
All of *.users.example.com requests should go to a particular controller/action. How do I specify this in routes.rb?
All other requests to www.example.com go to the normal list of routes in my routes.rb file.
UPDATE: I watch the railscast about subdomains and it showed the following bit of code which would seem to be exactly what I need (changed the controller and subdomain):
match '', to: 'my_controller#show', constraints: {subdomain: /.+\.users/}
The problem is it only matches the root URL. I need this to match EVERY possible URL with a *.users subdomain. So obviously I would put it at the top of my routes.rb file. But how do I specify a catch-all route? Is it simply '*'? Or '/*'?
I think, you just need to do the following :
create a class Subdomain in lib :
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.host.include?('.users')
end
end
and in your routes :
constraints Subdomain do
match '', to: 'my_controller#show'
end
You can constraint route dynamically based on some specific criteria by creating a matches? method
Lets say we have to filter sub domain of URL
constraints Subdomain do
get '*path', to: 'users#show'
end
class Subdomain
def self.matches?(request)
(request.subdomain.present? && request.subdomain.start_with?('.users')
end
end
What we are doing here is checking for URL if it start with sub domain users then only hit users#show action. Your class must have mathes? method either class method or instance method. If you want to make it a instance method then do
constraints Subdomain.new do
get '*path', to: 'proxy#index'
end
you can achieve same thing using lambda as well like below.
Instead of writing class we can also use lambdas
get '*path', to: 'users#show', constraints: lambda{|request|request.env['SERVER_NAME'].match('.users')}

Rails 3 - How to escape from subdomain route to the normal route?

I've worked on making a multiple subdomains feature for my E-commerce website. So far, I've made it possible to assign a ':subdomain' attribute for every user and display their sites on like 'subdomain.example.com'.
But, I cannot return to 'example.com' from 'subdomain.example.com' because the 'root_path' leads to not 'example.com' but 'subdomain.example.com'.
routes.rb file:
constraints(Subdomain) do
match "/" => 'contributors#show'
end
root :to => "items#index"
The Subdomain class comes from 'domains.rb' file below.
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end
contributors#show
<%= link_to 'Home', root_path # this leads to contributor#show. wanna make it to index#show. %>
Any help is welcome. Thanks.
Have you tried root_url(:subdomain => false)? This strips the link of the subdomain and should do what you want.
Rails takes the first matching path defined in the routes.rb file. If you are on the subdomain the constraint matches and match "/" matches as root path.
Try move "root :to => 'items#index'" to the top of your routes.rb file.

Resources