【发布时间】:2016-06-29 06:15:11
【问题描述】:
在使用--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 和对应的类级 respond_to 已经 移至响应者宝石。将 gem 'responders', '~> 2.0' 添加到您的 使用它的 Gemfile:
实例级 respond_to 不受影响:
我正在调用实例方法。怎么了?
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
【问题讨论】:
-
“怎么了?” – 嗯,发行说明是针对 4.2 的,而您使用的是 5?
-
@JörgWMittag,自 4.2 以来一直没有提及更改 meme-responds,因此它仍然非常相关。
-
您可能想尝试响应者 gem。真是太棒了。
-
@max,有什么宝石不好看吗?它让你屏住呼吸,让你大喊“哇哇哇哇,太棒了,哇哇哇哇!!!”还是什么?
-
有很多只是 meh 的宝石。 Responders 真的可以让你跳过一堆样板文件并干掉你的控制器。我特别推荐它用于 API。
标签: ruby-on-rails ruby rest ruby-on-rails-5