Why does Rails 4.2 + responders keeps telling me to add responders to the Gemfile?

断了今生、忘了曾经 提交于 2019-11-29 10:43:10

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.

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.

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 extending 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.

If your controller inherits from ActionController::Metal you may also need to include ActionController::RespondWith given you added the responders gem.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!