Rails 4 subdomain routing - ruby-on-rails

I have the following in my routes.rb file
namespace :client, path: '/' do
get '/' => 'default#index', as: :default_index
end
namespace :api, path: '/', constraints: { subdomain: 'api' } do
get '/' => 'default#index', as: :default_index
end
I have mounted a subdomain call "test.dev", and I can access "test.dev" without problems. However, if I try to get "api.test.dev" I get the same page as in "test.dev", unless I change the path for the namespace with the subdomain constraint.
Can't I have the same path for both even though one is on a subdomain and the other one isn't?

#Rodrigo, I've just faced the same problem, and I figure out the solution \o/.
The problem is in your "api.test.dev". I'm not dive into the details of why's, but it should be "api.test-dev.com", so append ".com" to your etc/hosts and change it to be like that:
api.test.dev
to
api.test-dev.com

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

Rails routes and subdomain: unknown url to 404

I am having an issue with routes, have tried to google but nothing came out so far. Would be great, if someone will able to explain, how works and solution.
Thanks,
Cheers :)
Sample:
1)
default url: lvh.me:3000/restaurants
Default url works fine, but once adding any unknown subdomain.
adding unknown subdomain: blabla.lvh.me/restaurants
It still visits lvh.me/restaurants and url shows with subdomain.
ok, lets add known subdomain from route:
platform.lvh.me/restaurants
It still visits lvh.me/restaurants and url shows with subdomain.
The admin and platform subdomains are react the same way.
2)
The same thing happens on :mobile and other extra routes
Route:
namespace :admin, path: '/' do
root to: 'pages#index'
end
constraints subdomain: 'platform' do
namespace :platform, path: '/' do
resources :categories
root 'pages#index'
end
end
resources :restaurants
root 'pages#index'
end
Rails routes by default searches the given URL to find from all routes, no matter subdomain, constraints...
Sample:
default url: lvh.me/restaurants
if to use any subdomain admin.lvh.me/restaurants, It still finds and renders from default url. Admin subdomain dnt have restaurants url.
Solution:
Need to add after every subdomain: match '*a' => redirect(path: '/'), via: :get
or render other pages like 404/500: match '*a', :to => 'errors#not_found', via: :get
Otherwise, will go further and renders from default url.
namespace :admin, path: '/' do
root to: 'pages#index'
match '*a' => redirect(path: '/'), via: :get
end
constraints subdomain: 'platform' do
namespace :platform, path: '/' do
resources :categories
root 'pages#index'
match '*a' => redirect(path: '/'), via: :get
end
end
resources :restaurants
root 'pages#index'
match '*a' => redirect(path: '/'), via: :get
end

add subdomain if namespace present

Not sure if my topic title is correct, but here is my question
I have namespace called :admin, so it looks like mysite.com/admin. In this section i have some links, that pointing to controllers inside this namespace. But since we have subdomain admin, and my namespace :admin as well, i'd like to all links that are being generated by routes.rb to prepend string admin., so the link would look like admin.mysite.com/admin/some_other_path
I've tried to add constraints to routes.rb, but that didn't work for me
But since we have subdomain admin, and my namespace :admin as well,
i'd like to all links that are being generated by routes.rb to prepend
string admin.
Routes
In your routes, you should have this:
constraints({ subdomain: "admin" }) do
namespace :admin do
# routes here
end
end
If you wanted to have no path for your admin namespace (I.E admin.domain.com/some_other_path), you can do this:
constraints({ subdomain: "admin" }) do
namespace :admin, path: "" do
# routes here
end
end
--
URL
When using URLs, you have to use the _url helpers (not _path). We literally just discovered this yesterday - the _path helpers only work to append relative paths to your url; the _url gives you a totally fresh url
This means if you have a route as follows:
admin_root_path "admin/application#index, constraints => {subdomain: "admin"}
You'll call this with this route helper:
<%= link_to "Admin", admin_root_url %>
This will prepend the required subdomain for you when calling links etc
you can do:
constraints subdomain: 'admin' do
namespace :admin do
# ...
end
end
Define routes in routes.rb under admin namespace like this
namespace :admin, path: '/', constraints: { subdomain: 'admin' } do
constraints(Subdomain) do
# your routes
end
end
Routes defined under this block will always come under admin in link, e.g., /admin/some_other_path
To add subdomain to the admin namespace take a look at this question
Rails namespace admin on custom subdomain

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!

Prefix url helpers in subdomain constraints without having the prefix in the URL

Here is my routes.rb file :
constraints subdomain: 'pro' do
scope module: 'pro' do
resources :subjects
end
end
This will generate:
subjects GET /subjects(.:format) pro/subjects#index {:subdomain=>"pro"}
How can I have the pro_subject url helper without having the '/pro' prefix in the URL as I already have the subdomain.
I want people to type: http://pro.mysuperwebsite.com and not http://pro.mysuperwebsite.com/pro
I'm pretty sure if you set the :path option, you can determine the subpath.
namespace :pro, :path => '' do # This should do it

Resources