60

In rails 5 created with --api I have an error

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

However, in the documentation for rails 4.2 it says 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:

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
max
  • 96,212
  • 14
  • 104
  • 165
Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • 4
    "What's the matter?" – Um, the release notes are for 4.2 and you are using 5? – Jörg W Mittag Mar 14 '16 at 09:25
  • @JörgWMittag, there has not been any mention of changing meme-responds since 4.2 so it is still very relevant. – max Mar 14 '16 at 09:33
  • 1
    You might want to try the responders gem though. Its pretty damn awesome. – max Mar 14 '16 at 09:34
  • 1
    @max, is there any gem that's not awesome? It takes your breath away and makes you yell "waayyyyy, it's pretty awesome, yyyaaahhhhh!!!" or what? – Incerteza Mar 14 '16 at 09:48
  • There are plenty of gems that are just *meh*. Responders really lets you skip a bunch of boilerplate and DRY out your controllers. I especially recommend it for API's. – max Mar 14 '16 at 09:59
  • @max, instead of writing 2 lines of code I should use a gem? – Incerteza Mar 14 '16 at 10:17

2 Answers2

134

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


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

Source: ActionController::API docs

max
  • 96,212
  • 14
  • 104
  • 165
19

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-to https://github.com/plataformatec/responders

jpalmieri
  • 1,539
  • 1
  • 15
  • 25
  • 8
    Quote from linked source "Instance-level respond_to is unaffected". If like OP your only using the instance-level respond_to you dont need the responders gem, just make sure ActionController::MimeResponds has been included in your controller. – complistic Jan 24 '17 at 04:38