Subdomains and Resources In Rails 3 - ruby-on-rails

I Have a site that is split into three units(subdomains):
example.com # Main Site
archive.example.com # Searchable Archive
admin.example.com # CMS
At the bottom of config/routes.rb I'm mapping the subdomains and root as follows:
match "/" => "archive#index", constraints: {subdomain: "archive"}
match "/" => "admin#index", constraints: {subdomain: "admin"}
root :to => "pages#index
I have a number of resources which are currently declared like:
resources :users
resources :themes
resources :downloads
With this setup, the resources are available in all subdomains, so for the users resource the following are all valid:
archive.example.com/users
admin.example.com/users
example.com/users
How do I set up my routes so that users are only available under an admin subdomain?
Visiting archive.example.com/users or example.com/users should result in a Routing Error.

This should do the trick:
constraints :subdomain => "admin" do
resources :users
end

Related

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

Rails 4 static subdomain

I can't set root path for subdomain.
This is my routes.rb part:
constraints subdomain: 'blog' do
scope :module => "blog", :as => "blog" do
resources :posts
end
end
root 'statics#index'
When I am visiting blog.example.com I've got static#index action response and get posts#index, when visiting blog.example.com/posts.
I want to set root path for blog.example.com pointing to posts#index.
No effect for this:
match '/' => 'posts#index', :constraints => { :subdomain => 'blog' }, via: [:get]
I believe you should still be able to call root within the constraints block:
constraints subdomain: 'blog' do
root :to => 'posts#index' # Perhaps this needs to be `blog/posts#index`?
scope :module => "blog", :as => "blog" do
resources :posts
end
end
root 'statics#index'
This works for me with separate namespaces for each subdomain.

Rails 3 + devise - login at specific subdomain

I have Rails 3.2 app with Devise gem and i18n. But i would like to improve a little my application and allow users to login at such subdomain: login.my-address.com. But i can't figure how to do that. I understand that i should point somehow to devise/sessions#new and i tried few things which didn't help me. Did anyone create such solutions?
My routes:
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
resources :articles
resources :photos
...
root :to => 'articles#index'
end
match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" } # i18n.
match '', to: redirect("/#{I18n.default_locale}/articles") # Redirect by default to articles.

How do I map a resource the root of an application in Rails 3?

In routes.rb, I'm using resources to declare all common routes for a controller:
resources :photos
This creates URLs which look like this:
http://example.com/photos
http://example.com/photos/new
...
How do I remove photos from the URLs? That is, how do I map this controller to the root of the application?
You can route a resourceful controller to the root by adding the path option:
resources :photos, :path => "/"
And, of course, this can still be extended in the usual way;
resources :photos, :path => "/" do
member do
get 'view_original_size'
end
end
For more information, see Module
ActionDispatch::Routing::Mapper::Resources > resources > Supported options in the API documentation.

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