创建 Rails 模型时出现语法错误 [英] Syntax error when creating a Rails model
问题描述
我正在使用以下命令在 Rails 3 中创建汽车模型:
I am creating Car model in Rails 3 by using command:
rails generate model Car name:string id_str:string
但我得到了错误:
/home/XX/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:234:in `load': /home/XX/myapp/config/initializers/session_store.rb:3: syntax error, unexpected ':', expecting $end (SyntaxError)
...sion_store :cookie_store, key: '_myapp_session'
为什么?
我的session_store.rb
Myapp::Application.config.session_store :cookie_store, key: '_myapp_session'
我的 session_store.rb
有什么问题?
推荐答案
当您尝试将 Ruby 1.9.2 Hash 语法与 Ruby < 一起使用时,通常会发生此错误.1.9.
This error usually happens when you are trying to use the Ruby 1.9.2 Hash syntax with Ruby < 1.9.
在 Ruby 1.9.2 中,以下代码完美运行
In Ruby 1.9.2 the following code works perfectly
Myapp::Application.config.session_store :cookie_store, key: '_myapp_session'
在 Ruby 中
1.9 你必须使用
while in Ruby < 1.9 you must use
Myapp::Application.config.session_store :cookie_store, :key => '_myapp_session'
这很奇怪,因为您的堆栈跟踪引用了 Ruby 1.9.2.您确定使用 Ruby 1.9.2 运行生成器吗?
This is strange, because your stack trace references Ruby 1.9.2. Are you sure you are running the generator with Ruby 1.9.2?
在任何情况下,您都可以将 session_store.rb
文件转换为 hash-rocket 语法.如果它有效,则意味着您没有使用 Ruby 1.9.2 并且您粘贴了无效的错误消息.
In any case, you can convert your session_store.rb
file to the hash-rocket syntax. If it works, it means you are not using Ruby 1.9.2 and you pasted an invalid error message.
这篇关于创建 Rails 模型时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!