为什么Rails 4.2 +响应者一直告诉我向Gemfile添加响应者?

时间:2014-12-10 23:15:21

标签: ruby-on-rails ruby ruby-on-rails-4.2

我将Rails 4.1.8应用程序(也使用rails-api~> 0.3.1)升级到4.2.0.rc2,并希望保留respond_with功能。我已将responders添加到Gemfile中,但当我bin/rake spec时,我得到:

/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>'

为什么我可能会得到这个? ......当然,我怎么能绕过它呢?

相关文件:

# 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

编辑:毫不奇怪,这种情况发生在我手动发出请求时,而不仅仅是在测试期间。

5 个答案:

答案 0 :(得分:13)

以下使用rails-api / active_model_serializers 0.8.3

为我工作

删除

include ActionController::MimeResponds
include ActionController::ImplicitRender

添加

include ActionController::RespondWith

请参阅github上的this discussion

答案 1 :(得分:3)

为清楚起见,如果控制器继承自ActionController::API并且包含ActionController::MimeResponds,则看起来可以重现该问题。为了避免控制器级respond_to的错误,我已经这样做了:

class ApplicationController < ActionController::API
  include ActionController::MimeResponds

  def self.respond_to(*mimes)
    include ActionController::RespondWith::ClassMethods
  end

  respond_to :json

end

我没有看到如何解决respond_with的问题,但它会有点不同,因为它是一个实例方法。

答案 2 :(得分:1)

看起来是responders / rails-api不兼容。我在responders :location中尝试了ApplicationController,并获得undefined method 'responders' for ApplicationController:Class (NoMethodError)的回溯,让我相信responders gem将responders类方法添加到ActionController::Base 1}}。由于rails-api您的控制器继承自ActionController::API,因此responders方法实际上不会添加到我的ApplicationController

确认:responders/lib/responders/controller_method.rb

我尝试使用extend ApplicationController Responders::ControllerMethod rails-api,但这并没有解决问题。

我的解决方案实际上是使用ApplicationController < ActionController::Base,然后使用{{1}}。

答案 3 :(得分:1)

如果您的控制器继承自ActionController::Metal,您可能还需要include ActionController::RespondWith,因为您添加了响应者宝石。

答案 4 :(得分:0)

我刚刚将gem响应器添加到我的gemfile中做了一个bundle install然后我做了一个rails生成响应器:install并安装了这些文件。  之后我运行了我的rails生成脚手架食品名称:字符串卡路里:浮点数和db:drop db:create和db:migrate(删除并刷新数据库)我停止收到错误。

基本上,当我在运行rails之前安装gems和文件生成scaffold 无论它开始工作。