0
0

More than 3 years have passed since last update.

【Ruby on Rails】NoMethodError: undefined method `t' for #<ActiveRecord::Migration:〇〇〇〇>エラーの対処

Last updated at Posted at 2020-11-14

エラー画面スクリーンショット 2020-11-15 7.34.48.png

該当のソースコード

class AddDeviseToUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.string :name,          null: false
      t.text :profile
    end
  end
  t.string :email,              null: false, default: ''
  t.string :encrypted_password, null: false, default: ''
end

原因

メソッドtの定義はそのcreate_tableのdoに対するendまでの範囲でしか使えない

解決

class AddDeviseToUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.string :name,               null: false
      t.text   :profile
      #下の二行をdoの中に入れた
      t.string :email,              null: false, default: ''
      t.string :encrypted_password, null: false, default: ''
    end
  end
end

補足

rake db:migrateしてもSchema.rbに反映されない方はrake db:migrate:rollbackで一度マイグレーションを落としてからマイグレーションを変更してrake db:migrateしてください。

【参考資料】https://qiita.com/s_tatsuki/items/3e1f119c91e21b8f0c33

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0