BaracudaBill BaracudaBill - 27 days ago 11
Ruby Question

MySQL Syntax error: unexpected keyword_do_block

I am very new to Ruby and trying to complete a tutorial that I can't get to work properly. I am attempting to run

rake db:migrate
on my root folder and is giving me 3 separate error messages:

>rake db:migrate
rake aborted!
SyntaxError:
C:/Users/Bill/Sites/simple_cms/db/migrate/20170922050429_create_use
rs.rb:4: syntax error, unexpected keyword_do_block
create_table :users, do |t|
^
C:/Users/Bill/Sites/simple_cms/db/migrate/20170922050429_create_users.rb:5:
syntax error, unexpected tSTRING_BEG, expecting keyword_end
t.column "first_name", :string, :limit => 25
^
C:/Users/Bill/Sites/simple_cms/db/migrate/20170922050429_create_users.rb:5:
syntax error, unexpected ',', expecting keyword_end
t.column "first_name", :string, :limit => 25
^

Tasks: TOP => db:migrate
(See full trace by running task with --trace)


The Ruby code from my
create_users.rb
file is:

class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users, do |t|
t.column "first_name", :string, :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => "", :null => false
t.string "password", :limit => 40
t.timestamps
end
end


I am not sure what I am doing wrong here. Any insight would be appreciated!

Answer Source

In your code:

def change
  create_table :users, do |t|
    t.column "first_name", :string, :limit => 25
    t.string "last_name", :limit => 50
    t.string "email", :default => "", :null => false
    t.string "password", :limit => 40
    t.timestamps
  end
end

You have:

create_table :users, do |t|

which should be:

create_table :users do |t|

There should not be any comma after that unless you have more than one arguments there.

Update:

And also you class has no end which will not close the class definition and throw exception.

On a side note:

You have this line:

t.column "first_name", :string, :limit => 25

which can be written as:

t.string "first_name", :limit => 25

Hope this helps.