简体   繁体   中英

Rails 4, subdomain routing

Trying to implement web service in rails through API sub-domain called "api".
In my hosts file i added line: 127.0.0.1 api.localhost

In my routes.rb i set sub-domain, where i only need index action and few manually added restful routes, through following:

namespace :api, path: '', :constraints => {:subdomain => "api"} do
  resources :posts, only: :index do
    collection do
      get 'popular_by_day'
      get 'popular_by_week'
    end
  end
end

Also generated coresponding controller with: rails g controller api/posts

Test example:

class Api::PostsController < ApplicationController
  def index
    @posts = 1

    respond_to do |format|
        format.json  { render :json => @posts }
    end
  end

  def popular_by_day
  end

  def popular_by_week
  end
end

After rake routes i have following:

popular_by_day_api_posts GET  /posts/popular_by_day(.:format)  api/posts#popular_by_day {:subdomain=>"api"}
popular_by_week_api_posts GET  /posts/popular_by_week(.:format) api/posts#popular_by_week {:subdomain=>"api"}
                api_posts GET  /posts(.:format)                 api/posts#index {:subdomain=>"api"}

So far as i know, link to http://api.localhost:3000/posts should work but i get routing error:
No route matches [GET] "/posts" (Same with /posts.json)

Try http://api.lvh.me:3000 for Rails 3+

From Ryan Bates's railscasts :

If we go to http://lvh.me:3000/ we'll see the homepage of our application because lvh.me resolves to the IP address 127.0.0.1. The difference is that we can now prepend any subdomain we like to the URL and it will still point to the same application.

EDIT: I would love to know in the comments why whoever downvoted this answer did so. This approach continues to work for me.

EDIT II: updated the link to Rails Casts from ASCII casts and added note about Rails 3+

So I'm posting my research on the topic

By default, Rails has its TLD Length set to 1. As a result, it won't be able to determine the subdomain in api.localhost , hence, the routing error.

You have several solutions:
- set up a correct domain that points to 127.0.0.1 with a tld length of 1 (eg dev-mywebsite.com)
- use lvh.me
- set config.action_dispatch.tld_length = 0 in your development.rb enrironment file

Hope this helps other people in the same need

Edit: It is actually not a good idea to use localhost so setup an api as a domain in development. I found out that it doesn't work with cookies across subdomains

@Srle It seems, I faced this yesterday.

Just try to add ".com" to your subdomain: api.localhost.com .

In my app it worked as well: api.myapplication-dev.com.

The main reason is that there isn't a .localhost domain, so put .com to your host :)

Hope it works for you as worked for me

You can achieve this with below steps:

1 – Login as an admin user on terminal: sudo su –

2 – Edit /etc/hosts file nano /etc/hosts

The above command will open /etc/hosts file for you in a terminal. You will find a few lines there, the important one is:

127.0.0.1 localhost

This is the line which basically maps localhost to IP 127.0.0.1, Add a new line below it:

127.0.0.1 admin.localhost

This is telling that the new DNS admin.localhost should also map to IP 127.0.0.1, press CTRL + x to exist editing It will ask you to save before existing. Press Y to save the change.

3 -restart your rails server. Now you can access your localhost at both the below URL http://localhost:3000/ and http://admin.localhost:3000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM