gpt4 book ai didi

ruby-on-rails - Rails 5 API Controller 中未定义的实例方法 "respond_to"

转载 作者:数据小太阳 更新时间:2023-10-29 06:22:09 24 4
gpt4 key购买 nike

在用 --api 创建的 rails 5 中我有一个错误

NoMethodError (undefined method `respond_to' for #<Api::MyController:0x005645c81f0798>
Did you mean? respond_to?):

然而,在 rails 4.2 的文档中它说 http://edgeguides.rubyonrails.org/4_2_release_notes.html

respond_with and the corresponding class-level respond_to have been moved to the responders gem. Add gem 'responders', '~> 2.0' to your Gemfile to use it:

Instance-level respond_to is unaffected:

我正在调用实例方法。怎么了?

class ApplicationController < ActionController::API
end

# ...
class Api::MyController < ApplicationController

def method1
# ...
respond_to do |format|
format.xml { render(xml: "fdsfds") }
format.json { render(json: "fdsfdsfd" ) }
end

最佳答案

ActionController::API 不包括 ActionController::MimeResponds 模块。如果您想使用 respond_to,您需要包含 MimeResponds

class ApplicationController < ActionController::API
include ActionController::MimeResponds
end


module Api
class MyController < ApplicationController
def method1
# ...
respond_to do |format|
format.xml { render(xml: "fdsfds") }
format.json { render(json: "fdsfdsfd" ) }
end
end
end
end

来源:ActionController::API docs

关于ruby-on-rails - Rails 5 API Controller 中未定义的实例方法 "respond_to",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35983628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com