【问题标题】:Rails API app: (No MethodError) when I run foreman startRails API 应用程序:(无 MethodError)当我运行工头启动时
【发布时间】:2012-12-30 05:58:45
【问题描述】:

我刚刚创建了一个新的 Rails API 应用程序并尝试第一次运行它。 我已经安装了工头 gem,我正在使用命令

  foreman start

启动服务器,但出现以下错误:

 Exiting
 21:52:55 web.1  |
 /Users/AM/Documents/RailsWS/app1229/config/initializers/wrap_parameters.rb:8:in 
 `block in <top (required)>':
 undefined method `wrap_parameters' for ActionController::API:Class (NoMethodError)
 21:52:55 web.1  |  
 from /Users/AM/.rvm/gems/ruby-1.9.3-p194-gemset/gems/activesupport-
 3.2.8/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
 21:52:55 web.1  |  
 from /Users/AM/.rvm/gems/ruby-1.9.3-p194-gemset/gems/activesupport- 
 3.2.8/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
 21:52:55 web.1  |  
 from /Users/AM/.rvm/gems/ruby-1.9.3-p194-gemset/gems/activesupport-
 3.2.8/lib/active_support/lazy_load_hooks.rb:26:in `block in on_lo
  ..........

我已经尝试更新 gemset 并重新设置终端等。

但是没有任何效果。想知道是否有人可以提供有关如何继续诊断此错误来源的想法

谢谢


编辑 这是ApplicationController.rb的内容

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

 end

宝石文件:

 source 'https://rubygems.org'
 gem 'rails', '3.2.8'
 gem 'rails-api'
 gem 'pg'
 gem 'thin'
 gem 'foreman'
 gem 'rabl'
 gem "paperclip"
 gem 'aws-sdk'
 group :development do
gem 'annotate', :git=>'git://github.com/ctran/annotate_models.git'
gem 'debugger'
 end

错误指向此文件中的第 8 行:wrap_parameters.rb

 # Be sure to restart your server when you modify this file.
 #
 # This file contains settings for ActionController::ParamsWrapper which
 # is enabled by default.

 # Enable parameter wrapping for JSON. You can disable this by setting :format to an    
   empty array.
 ActiveSupport.on_load(:action_controller) do
   ERROR >>>>  wrap_parameters format: [:json]
 end

 # Disable root element in JSON by default.
  ActiveSupport.on_load(:active_record) do
  self.include_root_in_json = false
 end

【问题讨论】:

  • 你能确认ActionController::API是从ActionController::Base继承的吗?
  • 这是我的 application.rb 文件的内容: class ApplicationController
  • 如果你只是rails server而不是foreman start,输出是否相同?
  • 是:ruby-1.9.3-p194 AM@~/Documents/RailsWS/app1229 >rails s => Booting Thin => Rails 3.2.8 应用程序开始在0.0.0.0:3000 上开发 => 调用使用 -d 分离 => Ctrl-C 关闭服务器退出 /Users/AM/Documents/RailsWS/app1229/config/initializers/wrap_parameters.rb:8:in block in &lt;top (required)&gt;': undefined method wrap_parameters' for ActionController::API:Class (NoMethodError )
  • 这可能与 gemset 有关,但我不确定是什么

标签: ruby-on-rails ruby api foreman


【解决方案1】:

wrap_parameters 中的问题在 rails-api 的 0.0.3 版本中得到修复,但是对于之前生成的应用程序,我们必须用 initializers/wrap_parameters.rb 中的第一块代码替换这个:

ActiveSupport.on_load(:action_controller) do
  include ActionController::ParamsWrapper
  wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
end

我现在已经在我的一个 heroku 应用程序中对其进行了测试,并且运行良好。不再需要在功能齐全的 Rails 中重写代码了 :)

【讨论】:

  • 这太完美了,我不得不说声谢谢,尽管你已经有一段时间没有回答这个问题了。谢谢!
【解决方案2】:

您的 Rails 项目目录的根目录中是否有 Procfile?对于最简单的设置,我的 Procfiles 通常如下所示:

web: bundle exec thin start -p $PORT

(当然,您的 Gemfile 中也需要 gem 'thin'。)

【讨论】:

  • 当我收到错误消息时,这些是我的 PROC 文件的内容:“web: bundle exec rails server thin -p $PORT -e $RACK_ENV”我还安装了瘦 gem
  • 尝试替换我上面的配置,看看是否有什么不同。
  • 不幸的是,仍然遇到同样的错误。这和我的宝石有什么关系吗?
  • 尝试删除 ApplicationController 中包含的每个模块,一次一个,直到不再出现错误。我怀疑其中一个模块正在干扰。
  • 嗯。似乎不起作用。由于某种原因,如此令人沮丧。我不知道该怎么办:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 2012-11-09
相关资源
最近更新 更多