11
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Rails】localhost:3000でsubdomainのurlを叩く方法

Posted at

routes.rb

constraints subdomain: 'api' do
  get 'status' => 'status#index'
  resources :users
end

rake routes

     users GET    /users(.:format)          users#index {:subdomain=>"api"}
           POST   /users(.:format)          users#create {:subdomain=>"api"}
  new_user GET    /users/new(.:format)      users#new {:subdomain=>"api"}
 edit_user GET    /users/:id/edit(.:format) users#edit {:subdomain=>"api"}
      user GET    /users/:id(.:format)      users#show {:subdomain=>"api"}
           PATCH  /users/:id(.:format)      users#update {:subdomain=>"api"}
           PUT    /users/:id(.:format)      users#update {:subdomain=>"api"}
           DELETE /users/:id(.:format)      users#destroy {:subdomain=>"api"}

ちゃんと subdomain => "api" が追加されています。

users_controller

特殊なことはしていません。

class UsersController < ApplicationController
  def index
    render :text => 'ok!'
  end
end

etc/hostsを開く

こいつにsubdomainを追加しないとダメなんです。

$ vim etc/hosts

etc/hostsに以下のスクリプトを追加

127.0.0.1 api.localhost.local

http://api.localhost.local:3000/usersを叩く

"ok!"

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?