RoR:当语法看起来正确时,为什么我的迁移失败。

时间:2012-08-13 05:15:01

标签: ruby-on-rails-3

alex@alex-ThinkPad-T410:~/rails_projects/final$ rake db:migrate
rake aborted!
/home/alex/rails_projects/final/db/migrate/20120813025503_add_price_location_and_product_to_microposts.rb:3: syntax error, unexpected ':', expecting ';' or '\n'
    add_column :microposts, :price, :text # d...
                ^
/home/alex/rails_projects/final/db/migrate/20120813025503_add_price_location_and_product_to_microposts.rb:3: syntax error, unexpected ',', expecting tCOLON2 or '[' or '.'
...add_column :microposts, :price, :text # dont forget to chang...
...                               ^
/home/alex/rails_projects/final/db/migrate/20120813025503_add_price_location_and_product_to_microposts.rb:7: syntax error, unexpected keyword_end, expecting $end

这是错误。这是迁移(下面)

class AddPriceLocationAndProductToMicroposts < ActiveRecord::Migration
  def 
    add_column :microposts, :price, :text 
    add_column :microposts, :location, :text
    add_column :microposts, :product, :text
  end
end

我写下了这个http://guides.rubyonrails.org/migrations.html#changing-migrations部分。 2.2

为什么要分号?我正在尝试将价格,位置,产品列添加到微博表

1 个答案:

答案 0 :(得分:2)

您必须在def

前面编写方法名称

即。将def更改为def change

class AddPriceLocationAndProductToMicroposts < ActiveRecord::Migration
  def change
    add_column :microposts, :price, :text 
    add_column :microposts, :location, :text
    add_column :microposts, :product, :text
  end
end