Home > Software engineering >  undefined method `respond_to' for Api::V1::LocationsController:Class
undefined method `respond_to' for Api::V1::LocationsController:Class

Time:10-22

I am new to rails and decided to make make an application that shows the weather. I followed this tutorial and GoRails Rails API video course. When I run server (path: http://127.0.0.1:3000/api/v1/locations), I see undefined method respond_to for Api::V1::LocationsController:Class error. The same is with http://127.0.0.1:3000/api/v1/locations/1. I googled a lot, but didn't see an appropriate answer. I am leaving code here. Locations is for the city and Recordings is for temperature. I am also leaving schema here if this is the case. A screen of directories tree is attached.

I'VE NEVER BEEN SO STUCK

routes.rb

Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :locations
    end
  end

  resources :locations
  root "locations#index"
end

locations_controller:

module Api
  module V1
    class LocationsController < ApplicationController
      respond_to :json

    def index
      respond_with LocationsController.all
    end

    def show
      respond_with find_location
    end

    private
    def find_location
      @location = LocationsController.find(params[:id])
    end
  end
  end
end

recordings_controller

class RecordingsController < ApplicationController
end

application_controller

class ApplicationController < ActionController::Base
end

Models:

location.rb

It is also not able to find these fields in db for some reason...

class Location < ApplicationRecord
  belongs_to :recording 

  validates_uniqueness_of :name
end

recording.rb

class Recording < ApplicationRecord
  has_many :locations
end

schema:

ActiveRecord::Schema[7.0].define(version: 2022_10_20_135036) do
  create_table "locations", force: :cascade do |t|
    t.integer "recording_id", null: false
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["recording_id"], name: "index_locations_on_recording_id"
  end

  create_table "recordings", force: :cascade do |t|
    t.integer "temp"
    t.string "status"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_foreign_key "locations", "recordings"
end

I thought to use here one-to-many association. As I thought, one city has one temperature and one temperature has loads of cities.

CodePudding user response:

The tutorial you are following is quite old and partly outdated. The respond_to method which is in the first line of the Api::V1:: LocationsController was removed in Ruby on Rails 5.0 about six years ago.

But the good news it that its functionality has been extracted to the responders gem. That means when you add that gem to your application's Gemfile then it might work after running bundle install and restarting the server.

Note. I wrote it might work because it is possible that there are other incompatibilities between the tutorial and your current, up-to-date Ruby on Rails version 7.0.

  • Related