【问题标题】:get :index, undefined method `respond_with' for Api::V1::EventsController为 Api::V1::EventsController 获取:index,未定义的方法 `respond_with'
【发布时间】:2017-05-03 12:05:57
【问题描述】:

http://localhost:3000/api/v1/events 发送获取请求以测试我的 API 是否有效时,我收到以下错误。

     Failure/Error: respond_with Event.all

     NoMethodError:
       undefined method `respond_with' for #<Api::V1::EventsController:0x007ff018f2edc8>
       Did you mean?  respond_to

我的 EventsController 看起来像这样

class Api::V1::EventsController < ApplicationController
  respond_to? :json

  def index
    respond_with Event.all
  end

我做错了什么?

【问题讨论】:

  • 试试render json: Event.all而不是respond_with

标签: ruby-on-rails rest api


【解决方案1】:

去掉“?” 在响应? :json

【讨论】:

  • 这给了我undefined method respond_to' for Api::V1: :EventsController:Class (NoMethodError)`
  • 你使用的是 rails 5.0.在这种情况下,我认为您需要添加 include ActionController::MimeResponds see here
猜你喜欢
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
相关资源
最近更新 更多