How to map a subdomain to an existing controller action in localhost By Admin

I want to have a sub domain for this particular route

   get 'product_list/home'

Previously the url used to be like localhost:3000/product_list/home. Now I want the url to be store.dev:3000/product_list/home

I have modified the route like this

 constraints subdomain: 'store' do 
      get 'product_list/home'
    end

 

 


  •  Open
  •  27-07-2016
  •  5
  •  244

Answers ( 5 )

 Posted on 27-07-2016

Your example and the examples in most other answers are actually not using subdomains at all: If you look at the domain name store.dev then it actually has no subdomain, store.dev is a top-level/root domain. An example for a subdomain would be: sub.store.dev

If you change your entry in the /etc/hosts file from store.dev to store.local.dev then your code will work.

Update

To answer the second part of the question (restricting a route to only the root domain): you can achieve this by wrapping the routes that should only be available on the root domain in another constraints block with an empty subdomain:

 Posted on 27-07-2016

I think you need to add the subdomain also to the /etc/hosts file:

127.0.0.1   dev items.dev

 Posted on 27-07-2016

Try adding a wildcard subdomain to /etc/hosts:

127.0.0.1 *.dev
Alternatively, just navigate to http://items.lvh.me. (This domain's owner points all DNS requests for the domain and subdomains to 127.0.0.1 currently)

Also, you may need to bind to the correct interfaces when starting the server as 127.0.0.1 is not strictly equivalent to localhost (which is where the rails server binds to by default):

 Posted on 27-07-2016

Try adding a wildcard subdomain to /etc/hosts:

127.0.0.1 *.dev
Alternatively, just navigate to http://items.lvh.me. (This domain's owner points all DNS requests for the domain and subdomains to 127.0.0.1 currently)

Also, you may need to bind to the correct interfaces when starting the server as 127.0.0.1 is not strictly equivalent to localhost (which is where the rails server binds to by default):

 Posted on 27-07-2016

Request-Based Constraints

You can also constrain a route based on any method on the Request object that returns a String.

You specify a request-based constraint the same way that you specify a segment constraint:

get 'photos', to: 'photos#index', constraints: { subdomain: 'admin' }

You can also specify constraints in a block form:

namespace :admin do
  constraints subdomain: 'admin' do
    resources :photos
  end
end
UPDATE:

I found this link which provides a way to create subdomain routes on dev machines. If that also doesn't solve your problem, there's another way here.