Rails 4, subdomain routing

被刻印的时光 ゝ 提交于 2021-02-18 07:22:48

问题


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)


回答1:


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 (e.g. 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




回答2:


Try http://api.lvh.me:3000for 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+




回答3:


@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




回答4:


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



来源:https://stackoverflow.com/questions/20095318/rails-4-subdomain-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!