I\'m upgrading a Rails 4.1.8 app (also using rails-api ~> 0.3.1) to 4.2.0.rc2 and would like to retain the respond_with
functionality. I\'ve added respon
I just added gem responder to my gemfile did a bundle install and then I did a rails generate responder:install and that installed the files. After that I ran my rails generate scaffold food name:string calories:float and after db:drop db:create and db:migrate (dropped and refreshed the database) I stopped getting errors.
Basically when I installed the gems and the files before running rails generate scaffold whatever it started to work.
The following worked for me using rails-api
/ active_model_serializers 0.8.3
:
Remove
include ActionController::MimeResponds
include ActionController::ImplicitRender
Add
include ActionController::RespondWith
See this discussion on github.
Looks like it was a responders
/rails-api
incompatibility. I tried responders :location
in the ApplicationController
and would get a backtrace with undefined method 'responders' for ApplicationController:Class (NoMethodError)
, leading me to believe that the responders
gem adds the responders
class method to ActionController::Base
. Since rails-api
has your controllers inherit from ActionController::API
, the responders
methods wouldn't, in effect, get added to my ApplicationController
.
Confirmed: responders/lib/responders/controller_method.rb
I tried extend
ing my ApplicationController
with Responders::ControllerMethod
, but that didn't get me around the problem.
My solution, effectively, was to drop using rails-api
, then ApplicationController < ActionController::Base
.
For clarity, it looks like the issue can be reproduced if the controller inherits from ActionController::API
and ActionController::MimeResponds
is included. To avoid the error with controller-level respond_to
, I have done this:
class ApplicationController < ActionController::API
include ActionController::MimeResponds
def self.respond_to(*mimes)
include ActionController::RespondWith::ClassMethods
end
respond_to :json
end
I didn't look at how to solve the issue for respond_with, but it would be a little different as it is an instance method.
If your controller inherits from ActionController::Metal
you may also need to include ActionController::RespondWith
given you added the responders gem.