简体   繁体   English

生成项目后Ruby on Rails语法错误

[英]Ruby on Rails Syntax Error after generating project

I generated the project with the command rails new reservation_maker -d mysql and after it was finished generating I got 4 errors. 我用rails new reservation_maker -d mysqlrails new reservation_maker -d mysql命令生成了项目,完成生成后,我遇到了4个错误。 All were syntax error, unexpected ':' . 都是syntax error, unexpected ':' The first was in the gemfile on the line gem 'sdoc', require: false . 第一个是在gem 'sdoc', require: falsegem 'sdoc', require: false The second in application_controller.rb on the line protect_from_forgery with: :exception . 第二行在application_controller.rb中,行protect_from_forgery with: :exception The third was in session_store.rb on the line ReservationMaker::Application.config.session_store :cookie_store, key: '_reservation_maker_session' . 第三个是在session_store.rb中的ReservationMaker::Application.config.session_store :cookie_store, key: '_reservation_maker_session' The last in wrap_parameters.rb on the line wrap_parameters format: [:json] if respond_to?(:wrap_parameters) . 最后一行是wrap_parameters format: [:json] if respond_to?(:wrap_parameters)wrap_parameters format: [:json] if respond_to?(:wrap_parameters) So, what did I do wrong? 那么,我做错了什么?

Edit: I also looked back at a previous rails project and it has the same setup and lines of code as this one but none of the errors. 编辑:我还回顾了以前的Rails项目,它具有与此项目相同的设置和代码行,但没有错误。 Also I'm using ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux] and rails 4.0.2 另外我正在使用ruby 2.0.0p353(2013-11-22修订版43784)[x86_64-linux]和rails 4.0.2

I just replaced the : with => and it cleared the errors but when I try to run the server it tells me that there is an error in the gem file 我只是用=>替换了: ,它清除了错误,但是当我尝试运行服务器时,它告诉我gem文件中有错误

What does ruby -v return? ruby -v返回什么?

It looks like it's crashing on hashes declared like key: value . 看起来像在声明为key: value哈希值上崩溃了。 This is a new hash syntax support in ruby 1.9+. 这是ruby 1.9+中新的哈希语法支持。 You are probably running ruby 1.8.7 which only support hashes declared with arrows like :key => value . 您可能正在运行ruby 1.8.7,它仅支持用箭头声明的哈希,例如:key => value

You should install ruby 2.0 to work with new rails 4 projects. 您应该安装ruby 2.0才能使用新的Rails 4项目。

Earlier when I replaced the : with a => I didn't fully understand the difference between the two. 之前,当我用=>替换: ,我并不完全理解两者之间的区别。 After learning the the colon can only be used with symbols I moved the colon to the front and added the => to each of the lines of the code throwing an error. 学习完冒号只能与符号一起使用后,我将冒号移到了最前面,并在代码的每一行中添加了=>并引发了错误。

gem 'sdoc', require: false
protect_from_forgery with: :exception
ReservationMaker::Application.config.session_store :cookie_store, key: '_reservation_maker_session'
wrap_parameters format: [:json] if respond_to?(:wrap_parameters)

became 成为

gem 'sdoc', :require => false
protect_from_forgery :with => :exception
ReservationMaker::Application.config.session_store :cookie_store, :key => '_reservation_maker_session'
wrap_parameters :format => [:json] if respond_to?(:wrap_parameters)

After that it worked. 之后,它起作用了。 If anyone knows how to fix the behavior in rails could you please tell me. 如果有人知道如何解决Rails中的问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM