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

前端 未结 5 838
不思量自难忘°
不思量自难忘° 2020-12-19 06:16

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

相关标签:
5条回答
  • 2020-12-19 06:25

    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.

    0 讨论(0)
  • 2020-12-19 06:27

    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.

    0 讨论(0)
  • 2020-12-19 06:31

    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.

    0 讨论(0)
  • 2020-12-19 06:34

    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.

    0 讨论(0)
  • 2020-12-19 06:51

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

    0 讨论(0)
提交回复
热议问题