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"}
Far as i know, link to [http://api.localhost:3000/posts](http://api.localhost:3000/posts) should work but i get routing error:
**No route matches [GET] "/posts"** (Same with /posts.json)