basic rails api questions

102 views Asked by At

I'm just starting to learn to build api's in rails and have a question getting started. I just want to practice creating routes and controllers but can't get this to work

config/routes.rb

  namespace :api, :path => "", :defaults => {:format => :json}, :constraints => {:subdomain => "api"} do    
    get '/prac' => 'sessions#prac'
  end

controllers/api/sessions_controller.rb

def prac
  "hello world"
end

When I got to api.localhost:3000/prac, I get (No route matches [GET] "/prac"). I just want the hello world to show up so that I know I am on the right path. Any help would be appreciated.

1

There are 1 answers

0
ifyouseewendy On

it's the subdomain part.

subdomain(tld_length = @@tld_length)

Returns all the subdomains as a string, so "dev.www" would be returned for “dev.www.rubyonrails.org”. You can specify a different tld_length, such as 2 to catch "www" instead of "www.rubyonrails" in “www.rubyonrails.co.uk”.

In your case, as the subdomain of api.localhost is '', so this should pass

namespace :api, :path => "", :defaults => {:format => :json}, :constraints => {:subdomain => ""} do    
  get '/prac' => 'sessions#prac'
end

Or, you can use a normal local host, like api.local.com to make your subdomain check work.