Definity
Definity

Reputation: 722

Rails, Routes and REST

I'm trying to learn REST made a simple API with no security for testing, but I have got this error:

ActionController::RoutingError (No route matches [GET] "/api"):

And when I check the routes I have this.

Rails.application.routes.draw do
  resources :users do
    get 'search', on: :collection
  end



  # A singleton resource and so no paths requiring ids are generated
  # Also, don't want to support editing of the session
  resource :session, only: [:new, :create, :destroy]

  namespace :api, defaults: {format: :json} do
    resources :broadcast, only: [:create, :index] #Have I set this up wrong?
  end

  resources :broadcasts, except: [:edit, :update]

  get 'home', to: 'home#index', as: :home

  root 'home#index'

http://guides.rubyonrails.org/routing.html end I have made a little script in ruby to try and access it but I just get a 503 all the time, I'm using c9.io for development would that be the problem? Here is the ruby code I made for trying to list some broadcasts.

require "net/http"
require "uri"

uri = URI.parse("http://assignment-cloned-pas43.c9users.io")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new("/api/broadcasts/")
response = http.request(request)

puts response.code
puts response.body

Here is the layout of my directory's https://snag.gy/EVm3HN.jpg

Upvotes: 1

Views: 65

Answers (1)

Eric
Eric

Reputation: 2549

Check your routes with rake routes. Nesting :broadcast within :api means you probably have a route for the api_broadcast REST helper at /api/broadcast. Namespacing at api means that URL segment isn't going to have its own route, so /api by itself will predictably fail.

Upvotes: 3

Related Questions