尝试启动 Rails 服务器时出现语法错误

Posted

技术标签:

【中文标题】尝试启动 Rails 服务器时出现语法错误【英文标题】:Syntax error when trying to start rails server 【发布时间】:2012-06-05 06:32:21 【问题描述】:

我最近开始从事一个从 github 克隆的项目。

每次我尝试启动 rails 服务器时都会收到以下错误:

/Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in load': /Users/thomas/Projects/BillingMiddleware/config/initializers/session_store.rb:3: syntax error, unexpected ':', expecting $end (SyntaxError) ...sion_store :cookie_store, key: '_BillingMiddleware_session' ^ from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:inload' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in load_dependency' from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:inload' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/engine.rb:588 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/engine.rb:587:in each' from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/engine.rb:587 from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:30:ininstance_exec' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:30:in run' from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:55:inrun_initializers' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:54:in each' from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:54:inrun_initializers' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/application.rb:136:in initialize!' from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:insend' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in method_missing' from /Users/thomas/Projects/BillingMiddleware/config/environment.rb:5 from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:inrequire' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in require' from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:inload_dependency' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in require' from /Users/thomas/Projects/BillingMiddleware/config.ru:4 from /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:51:ininstance_eval' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:51:in initialize' from /Users/thomas/Projects/BillingMiddleware/config.ru:1:innew' 来自 /Users/thomas/Projects/BillingMiddleware/config.ru:1

我的同事也在做这个项目,启动服务器似乎没有任何问题。他和我运行相同版本的 ruby​​、rails 和 WeBrick。

我尝试将语法从 key: 更改为 :key =>,但这应该不是问题,因为我运行的是 ruby​​ 版本 1.9.3。如果我无论如何都进行更改,我会收到不同的错误消息:

/Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `load': /Users/thomas/Projects/BillingMiddleware/ config/initializers/wrap_parameters.rb:8: 语法错误,意外':',期待 kEND (SyntaxError) wrap_parameters 格式:[:json] ^ 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `load' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `load' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/engine.rb:588 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/engine.rb:587:in `each' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/engine.rb:587 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `instance_exec' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `run' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:55:in `run_initializers' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `each' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `run_initializers' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/application.rb:136:in `initialize!' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `send' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing' 来自 /Users/thomas/Projects/BillingMiddleware/config/environment.rb:5 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require' 来自 /Users/thomas/Projects/BillingMiddleware/config.ru:4 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval' 来自 /Users/thomas/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize' 来自 /Users/thomas/Projects/BillingMiddleware/config.ru:1:in `new' 来自 /Users/thomas/Projects/BillingMiddleware/config.ru:1

我已经厌倦了完全删除并重新安装 rvm、ruby 和 rails 几次都没有成功。我也试过运行 bundle install 但没有帮助。

有人知道出了什么问题吗?

谢谢

【问题讨论】:

【参考方案1】:

看起来像另一行的哈希语法,在你修复那个之后。检查config/intializers/wrap_parameters.rb,我想知道这个项目是否得到了“不”使用新语法的标志?

【讨论】:

我已经将两者都更改为不使用新语法,但似乎再次在项目中找到更多使用新语法的示例并抱怨它们。该项目是由我的同事自己创建的,所以我非常确定它不会有不使用新语法的标志。他似乎能够毫无问题地启动服务器。 你的ruby -v 说什么?您是否重新启动了控制台选项卡,可能发生了一些奇怪的事情。你在 Lion 上,他在 Snow Leopard 上? 当我运行 ruby -v 它返回 ruby​​ 1.9.3p194(2012-04-20 修订版 35410)[x86_64-darwin11.3.0]。每次我执行更新或安装某些东西时,我都会重新启动我的控制台。是的,我们的两台计算机都运行 10.7.3。【参考方案2】:

尝试将您的哈希更改为 :key => :value 语法。 Rails 3.1 Deployment to Heroku Error 讨论了相同的错误和修复。 建议的临时修复是: config/initializers/session_store.rb

App::Application.config.session_store :cookie_store, :key => '_BillingMiddleware_session'

config/initializers/wrap_parameters.rb

ActionController::Base.wrap_parameters :format => [:json]

【讨论】:

这似乎是解决问题的快速方法。我只是想知道为什么当我运行 Ruby 1.9.3 时它会抱怨这种哈希语法? 您仍在系统中以某种方式使用 ruby​​ 1.8.7。至少你已经启动并运行了。祝你好运。 很多代码已经提交到项目中,虽然有新的语法,但当它为项目的其他成员工作时,将其全部改回旧语法似乎毫无意义。这还有什么我可以尝试的吗?当我运行 ruby​​ -v 我得到 ruby​​ 1.9.3p194 (2012-04-20 修订版 35410) [x86_64-darwin11.3.0] 我正在使用 RVM 而当我运行 rvm list rubies 我只得到 =* ruby​​-1.9.3 -p194 [ x86_64 ] 我还运行了 rvm use ruby​​-1.9.3-p194 ,但没有帮助。 您的应用程序的其他部分是否也遇到同样的错误?我认为这只是初始化期间的问题,不会影响应用程序的其余部分。 最初是这种情况,但随着快速修复的实施,我注意到这个问题会影响应用程序中的文件,一旦它们由服务器运行。我的环境中是否还有其他地方可以检查是否默认返回旧版本的 ruby​​?

以上是关于尝试启动 Rails 服务器时出现语法错误的主要内容,如果未能解决你的问题,请参考以下文章

尝试运行 rails 时出现多个 gems 错误

尝试在 Rails 中发布文件时出现 405 错误

尝试迁移 postgreSQL 时出现或接近“WITH ORDINALITY”错误的语法错误

当我尝试生成脚手架时出现 Rails 错误

安装新的 Rails 应用程序时出现错误消息(安装 byebug 时出错)

访问 $GITLAB/admin/runners 时出现内部服务器错误 500