アレッ*_*ックス 46 ruby rest ruby-on-rails ruby-on-rails-5
在--api我创建的rails 5中有一个错误
NoMethodError (undefined method `respond_to' for #<Api::MyController:0x005645c81f0798>
Did you mean? respond_to?):
Run Code Online (Sandbox Code Playgroud)
但是,在rails 4.2的文档中,它说http://edgeguides.rubyonrails.org/4_2_release_notes.html
respond_with和相应的类级别respond_to已被移动到响应者gem.将gem'responseders','〜> 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
Run Code Online (Sandbox Code Playgroud)
max*_*max 88
ActionController::API不包括该ActionController::MimeResponds模块.如果你想使用respond_to你需要包括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
Run Code Online (Sandbox Code Playgroud)
jpa*_*eri 14
从Rails 4.2开始,此功能不再随Rails一起提供,但可以很容易地包含在响应者gem中(如Max在上面的评论中所述).
gem 'responders'然后添加到您的Gemfile
$ bundle install
$ rails g responders:install
Run Code Online (Sandbox Code Playgroud)
来源:
http
://edgeguides.rubyonrails.org/4_2_release_notes.html#respond-with-class-level-respond-to https://github.com/plataformatec/responders
| 归档时间: |
|
| 查看次数: |
25299 次 |
| 最近记录: |