Rails 4, subdomain routing

An attempt to implement a web service in rails through an API subdomain called "api".
In my file, hostsI added the line:127.0.0.1 api.localhost

In my routes.rbI installed a subdomain in which I only need an action with an index and several manually added recreation routes using the 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 a base controller with: rails g controller api/posts

Test case:

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 routesI have the 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"}

As far as I know, the link to http: //api.localhost: 3000 / posts should work, but I get a routing error:
There are no matches with the [GET] "/ posts" route (Same with /posts.json)

+5
4

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

railscasts:

http://lvh.me:3000/, , lvh.me IP- 127.0.0.1. , , URL- .

EDIT: , , , . .

EDIT II: โ€‹โ€‹ Rails Casts ASCII- โ€‹โ€‹ Rails 3 +

+9

,

Rails TLD, 1. api.localhost, , .

:
- , 127.0.0.1 tld 1 (, dev-mywebsite.com)
- lvh.me
- config.action_dispatch.tld_length = 0 development.rb enrironment

, .

: localhost, api . ,

+9

@Srle , .

".com" : api.localhost.com.

: api.myapplication-dev.com.

, .localhost, .com :)

, , .

+2

:

1 - : sudo su โ€“

2 - /etc/hosts nano /etc/hosts

/etc/hosts . , :

127.0.0.1 localhost

, localhost IP 127.0.0.1, :

127.0.0.1 admin.localhost

, DNS admin.localhost IP 127.0.0.1, CTRL + x, . , . Y, .

3 -restart your rails server. Now you can access your local host at the following URL http://localhost:3000/andhttp://admin.localhost:3000

0
source

All Articles