未定义的实例方法“respond_to"在 Rails 5 API 控制器中 [英] Undefined instance method "respond_to" in Rails 5 API Controller
问题描述
在用 --api
创建的 rails 5 中,我有一个错误
In rails 5 created with --api
I have an error
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
However, in the documentation for rails 4.2 it says http://edgeguides.rubyonrails.org/4_2_release_notes.html
respond_with 和对应的类级 respond_to 已经搬到了响应者宝石.将 gem 'responders', '~> 2.0' 添加到您的Gemfile 使用它:
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:
实例级 respond_to 不受影响:
Instance-level respond_to is unaffected:
我正在调用实例方法.怎么了?
And I'm calling the instance method. What's the matter?
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
.
ActionController::API
does not include the ActionController::MimeResponds
module. If you want to use respond_to
you need to include MimeResponds
.
class ApplicationController < ActionController::API
include ActionController::MimeResponds
end
class Api::MyController < ApplicationController
def method1
# ...
respond_to do |format|
format.xml { render(xml: "fdsfds") }
format.json { render(json: "fdsfdsfd" ) }
end
end
end
这篇关于未定义的实例方法“respond_to"在 Rails 5 API 控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!