Undefined instance method "respond_to" in Rails 5 API Controller Undefined instance method "respond_to" in Rails 5 API Controller ruby ruby

Undefined instance method "respond_to" in Rails 5 API Controller


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::MimeRespondsendclass Api::MyController < ApplicationController  def method1    # ...    respond_to do |format|      format.xml { render(xml: "fdsfds") }      format.json { render(json: "fdsfdsfd" ) }    end  endend

Source: ActionController::API docs


As of Rails 4.2, this functionality no longer ships with Rails, but can easily be included with the responders gem (like Max noted in comments above).

Add gem 'responders' to your Gemfile, then

$ bundle install$ rails g responders:install

Sources:
http://edgeguides.rubyonrails.org/4_2_release_notes.html#respond-with-class-level-respond-tohttps://github.com/plataformatec/responders