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

5

7

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 responders to the Gemfile, but when I bin/rake spec, I get:

/Users/sloveless/.gem/ruby/2.1.0/gems/actionpack-4.2.0.rc2/lib/action_controller/metal/mime_responds.rb:10:in `respond_to': The controller-level `respond_to' feature has been extracted to the `responders` gem. Add it to your Gemfile to continue using this feature: (NoMethodError)
  gem 'responders', '~> 2.0'
Consult the Rails upgrade guide for details.
    from /Users/sloveless/Development/twilight/app/controllers/application_controller.rb:6:in `<class:ApplicationController>'
    from /Users/sloveless/Development/twilight/app/controllers/application_controller.rb:1:in `<top (required)>'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `require'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `require'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:360:in `require_or_load'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:184:in `const_missing'
    from /Users/sloveless/Development/twilight/app/controllers/zone_maps_controller.rb:1:in `<top (required)>'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `require'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `require'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:360:in `require_or_load'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:184:in `const_missing'
    from /Users/sloveless/Development/twilight/spec/controllers/zone_maps_controller_spec.rb:3:in `<top (required)>'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `load'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `load'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `load'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `load'
    from /Users/sloveless/.gem/ruby/2.1.0/gems/spring-commands-rspec-1.0.3/lib/spring/commands/rspec.rb:18:in `call'
    from /Users/sloveless/.rubies/ruby-2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/sloveless/.rubies/ruby-2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

Any ideas why I might be getting this? ...and, of course, how I can get around it?

Pertinent files:

# app/application_controller.rb
class ApplicationController < ActionController::API
  include ActionController::MimeResponds
  include ActionController::ImplicitRender
  include SentientController

  respond_to :json

  # (Custom responder)
  self.responder = HTTPErrors::Responder
  before_action :set_locale

  private

  def set_locale
    I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
  end
end






# Gemfile
source 'https://rubygems.org'

gem 'rails', '4.2.0.rc2'
gem 'rails-api', '~> 0.3.1'

# Other utils
gem 'uuidtools'

# Serializing things
gem 'active_model_serializers', '~> 0.8.2'

# Persisting things
gem 'pg', '~> 0.17.1'
# gem 'activerecord-postgis-adapter', '~> 2.2.1'
# gem 'redis'

# Controller & routing things
gem 'http_accept_language', '~> 2.0.2'
gem 'kaminari'
gem 'rack-cors', require: 'rack/cors'
gem 'responders', '~> 2.0'

group :development do
  gem 'brakeman', require: false
  gem 'guard-bundler'
  gem 'guard-rails'
  gem 'guard-rspec'
  gem 'guard-rubocop'
  gem 'rails_best_practices'
  gem 'rubocop', require: false
  gem 'quiet_assets'
  gem 'web-console', '~> 2.0'
end

group :development, :test do
  gem 'bullet'
  gem 'byebug'
  gem 'factory_girl_rails'
  gem 'faker'
  gem 'pry-rails'
  gem 'pry-rescue'
  gem 'pry-stack_explorer'
  gem 'rspec-rails'
  gem 'puma'
  gem 'spring'
  gem 'spring-commands-rspec'
end

group :test do
  gem 'json_spec'
  gem 'shoulda-matchers'
end

group :production do
  gem 'newrelic_rpm'
end

Edit: To no surprise, this happens when I manually make a request, not just during testing.

Indiaindiaman answered 10/12, 2014 at 23:15 Comment(0)
P
13

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.

Pakistan answered 12/1, 2015 at 17:45 Comment(4)
This worked for me despite these lines being in another controller entirely—they were in an API controller in controllers/api/api_controller.rb.Accommodation
This works for me with some apps, but not all. Can't say for sure yet what the common failing denominator is, but this seems to be the right answer.Indiaindiaman
This solution does not work if you need the instance-level respond_to which is only declared in ActionController::MimeRespondsHaematite
This solution did not work for me. When I add include ActionController::RespondWith to my ActionController::API subclass it raises: uninitialized constant ActionController::RespondWithFinney
G
3

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.

Geotropism answered 13/12, 2014 at 1:48 Comment(0)
I
1

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.

Indiaindiaman answered 11/12, 2014 at 0:21 Comment(0)
Q
1

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

Quadrennium answered 31/12, 2014 at 17:37 Comment(0)
S
0

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.

Sidney answered 3/10, 2017 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.