How to handle routes in rails api -
i learning api on rails,and code below
routes.rb
require 'api_constraints' rails.application.routes.draw devise_for :users namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' scope module: :v1, constraints: apiconstraints.new(version: 1, default: true) resources :users, :only => [:show] end end end
users_controllers.rb
class api::v1::userscontroller < applicationcontroller respond_to :json def show respond_with user.find(params[:id]) end end
when run rails server localhost:3000/users/1
gives me error no route matches
checked routes using rake routes
, included in routes
`api_user /users/:id(.:format) api/v1/users#show'
but don't know why gives me error
api_constraints.rb
class apiconstraints def initialize(options) @version = options[:version] @default = options[:default] end def matches?(req) @default || req.headers['accept'].include?("application/vnd.marketplace.v#{@version}") end end
`
try following:
api.lvh.me:3000/api/v1/users/1
you've setup constraint requires api sub domain. you'll need remove constraint make following work:
lvh.me:3000/api/v1/users/1
note: lvh.me points 127.0.0.1
Comments
Post a Comment