Rails routes and subdomain: unknown url to 404 - ruby-on-rails

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

Related

Map routes into other routes in Rails

I'm having some troubles with routes in Ruby on Rails v5.2.0
Currently, I have a resource called users, so that I have a controller which takes actions (for example index) whenever I start my server in localhost on port 3000 and type in my browser
localhost:3000/users/
Is there an easy way to map the requests for this resource to the app root?Basically, I'm trying to achieve this:
localhost:3000/users/ --> localhost:3000/
localhost:3000/users/new/ --> localhost:3000/new/
This is how my routes.rb file looks like right now:
Rails.application.routes.draw do
devise_for :users
get 'landing/index'
get 'welcome/index'
resources :users
root to: 'landing#index'
end
Add the following lines to your routes.rb file
Change
root to: 'landing#index'
to
root "users#index"`
and add the line
get "/new" => "users#new"
Also if you want to learn more on routing, here is the link
http://guides.rubyonrails.org/routing.html
TLDR - Rails doesn't have a root model generator for routing
You can manually create the individuals routes
get :new, to: "users#new", as: "new_user"
...
However while using the rails generators resources you are just specifying a shorthand for
scope :model do
get :new, to: "model#new", as: "new_model"
...
end
You can checkout the rails guide to routing for more specifics on explicit creation
http://guides.rubyonrails.org/routing.html
HACKY SOLUTION
root to: "users#index", as: "users"
get :new, to: "users#new", as: "new_user"
post "/", to: "users#create"
scope ":id" do
root to: "users#show"
get :edit, to: "users#edit", as: "edit_user"
patch "/", to: "users#update"
...
end
It looks that what you want is to 'mute' users from the url. An option for this is to call path: '' on users like this:
Rails.application.routes.draw do
devise_for :users
get 'landing/index'
get 'welcome/index'
resources :users, path: '' # <-- HERE
root to: 'landing#index'
end
The value you give to path: is going to replace the resource name.
In this scenario users is being replaced with an empty string '', but it could be any other string.
This will remove users. However, you must consider that root to: 'landing#index AND users#index are both pointing to localhost:3000/
Without knowing your app, an option to solve this scenario, could be to have landing#index as root for gustes (not authenticated users) and users#index as a root for authenticated users.

Remove slug name from url locomotivecms

How can I remove slug name from URL? I have URL with parent page:
host_url/home/index
but I need this to be as
host_url/index #without parent slug name
You can simply change the routes as:
match '/index', :to => 'home#index', via: [:get]
If using resources in routes .
resources :users, path: '/'
get '/index' => 'home#index'

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.

Subdomains and Resources In Rails 3

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

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