thad
thad

Reputation: 167

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. All were syntax error, unexpected ':'. The first was in the gemfile on the line gem 'sdoc', require: false. The second in application_controller.rb on the line 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'. The last in wrap_parameters.rb on the line 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. Also I'm using ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux] and 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

Upvotes: 0

Views: 113

Answers (2)

thad
thad

Reputation: 167

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.

Upvotes: 0

Alex Wayne
Alex Wayne

Reputation: 187004

What does ruby -v return?

It looks like it's crashing on hashes declared like key: value. This is a new hash syntax support in ruby 1.9+. You are probably running ruby 1.8.7 which only support hashes declared with arrows like :key => value.

You should install ruby 2.0 to work with new rails 4 projects.

Upvotes: 1

Related Questions