2

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

In the /etc/hosts. I have added a store.dev as follows

127.0.0.1   localhost
127.0.0.1   store.dev

But when visited store.dev:3000 in dev environment, I am getting my homepage just like localhost:3000.

But I want to restrict my subdomain to only this route product_list/home.

And one more problem I am facing is

http://store.dev:3000/product_list/home when I visit this

I am getting this

No route matches [GET] "/product_list/home"

UPDATE:

After changing the entry in the hosts file .. from store.dev to store.local.dev it works I am able to access store.local.dev:3000/product_list/home. But my problem is I am able to access other pages also, say I have about page. store.local.dev:3000/about, but I don't want this to happen. How to restrict the subdomain to only one particular route

gates
  • 4,465
  • 7
  • 32
  • 60

6 Answers6

1

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:

constraints subdomain: '' do
  get '/about'
end
severin
  • 10,148
  • 1
  • 39
  • 40
  • Hi @severin, you fixed one problem of mine which is I am able to navigate to store.local.dev:3000/product_list/home. But one more problem I am facing is I want to restrict this subdomain `store` to only product_list/home. I should not be able to navigate to store.local.dev:3000/home .. that is where my home page resides. Right now I am able to navigate. – gates Jul 26 '16 at 02:05
  • @gates I updated my answer to also respond to your second question – severin Jul 26 '16 at 07:16
  • Thanks so much, I cannot award bounty for next 4 hours. I will do it, once I am allowed to award, and please point me to some useful resources on sub domain routing. – gates Jul 26 '16 at 07:46
1

Your routes file code is correct

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

For testing localhost use following url no need do change in host file store.lvh.me:3000

lvh.me is the virtual domain name mapped with 127.0.0.1

https://reinteractive.net/posts/199-developing-and-testing-rails-applications-with-subdomains

Vishal Zambre
  • 320
  • 1
  • 10
0

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

127.0.0.1   dev items.dev
zheath
  • 1
  • My point is not to have a home page which is pointing to items.dev:3000, instead items.dev:3000 should point to items#index action. – gates Jul 21 '16 at 16:06
0

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):

rails s -b 127.0.0.1

or

rails s -b 0.0.0.0 # all interfaces

Note that the latter may be considered unsafe as it exposes your development server to your local network.

Ivan
  • 235
  • 2
  • 6
  • Adding a subdomain like 127.0.0.1 to items.dev and navigating to items.dev:3000 works fine. But I don't want all the pages to have this subdomain, I just want to this particular page to have sub domain. I have home page configured in / . Now this part of the application also demands to be in / so going for sub domain is the only option for me. – gates Jul 22 '16 at 01:22
  • Then you should change your route to: `get '/' => 'items#index' , constraints: {subdomain: 'items'}'` – Ivan Jul 22 '16 at 07:09
  • It does not matter. You can have two get / as one of the has a subdomain constraint. Just make sure the route with the subdomain constraint is higher in the file so that it will be matched first. – Ivan Jul 22 '16 at 12:52
  • Hi, Please have a final look at my question. I am sorry to edit most of the question though – gates Jul 25 '16 at 12:42
0

From the Docs:

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.

Community
  • 1
  • 1
mansoor.khan
  • 2,309
  • 26
  • 39
  • Where is your server config? Have you mapped the subdomain to serve files from your project directory? – mansoor.khan Jul 25 '16 at 16:53
  • Downvoting an attempt to solve your problem won't help at all. What you wish to do here and what you have done are two different things. Give us a chance to help you, please. Which version of Rails are you using? – mansoor.khan Jul 25 '16 at 17:18
  • Hello steady_daddy, I did not downvote .. why would I downvote a person,who is trying to help me? But some other senior SO guy should have done it, because your answer shows that you have not read the question. – gates Jul 26 '16 at 02:03
0

I'm adding another answer as the question has changed from the original, and my existing answer is no longer relevant.

To ensure your subdomain store.domain.dev only has one available route, you need to wrap the rest of your routes in a constraint block too:

# this matches store.domain.dev
constraints subdomain: 'store' do 
  get '/product_list/home'

  # add a redirect for the root path so you don't get a 404 
  # when browsing to store.domain.dev (optional)
  root '/', to: redirect('/product_list/home')
end

# this matches no subdomain and 'www' subdomain (i.e. domain.dev and www.domain.dev)
constraints subdomain: /^(|www)$/ do
  # put ALL your other routes here
  # ..
  # root '/', to: 'home#index'
end

For the second part of the question, you should try using domain.dev instead of just dev as the subdomain may be interpreted wrongly otherwise.

Also, you should probably use get '/product_list/home' instead of just get 'product_list/home' as specified in the Rails routing guide: http://guides.rubyonrails.org/routing.html

Ivan
  • 235
  • 2
  • 6