![](/img/trans.png)
[英]Undefined instance method “respond_to” in Rails 5 API Controller
[英]Undefined method `respond_to' for Api::V1::Controller
我已經在這里討論了主題,但是提到的解決方案對我而言並不奏效。
我想為我的Rails應用程序創建一個API,然后按照Railscast中的教程進行API版本控制。 我正在使用Rails 5。
這是我的routes.rb
文件:
Rails.application.routes.draw do
namespace :api, defaults: { format: 'json' } do
namespace :v1 do
resources :orders
end
end
end
這是controllers/api/v1
下的新orders_controller.rb
:
module Api
module V1
class OrdersController < ApplicationController
respond_to :json
def index
respond_with Order.all
end
end
end
end
當訪問http://localhost:3000/api/v1/orders
我總是會收到以下異常:
undefined method `respond_to' for Api::V1::OrdersController:Class Did you mean? respond_to?
我已經在我的gemfile中添加了responder
,但是沒有成功。
在訂單控制器中,您犯了一個拼寫錯誤。 更改
repsond_with Order.all
至
respond_with Order.all
另外,您也在routes文件中犯了拼寫錯誤。 更改:
namespace :api, defaults: { format: 'josn' } do
至
namespace :api, defaults: { format: 'json' } do
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.