【问题标题】:Environment variables | Validation Errors when seeding with Heroku环境变量 |使用 Heroku 播种时的验证错误
【发布时间】:2018-01-21 06:37:08
【问题描述】:

我有一个 Rails 应用程序,我正在尝试在 Heroku 上播种数据。我正在运行 heroku run rake db:seed 并获得以下信息...

Validation failed: Email can't be blank, Password can't be blank

我真的不确定我在哪里出错了。本地一切运行良好,种子。只有 Heroku 才有这个问题。任何建议或意见,将不胜感激。

感谢您抽出宝贵时间,如果您需要更多信息,请告诉我。

我的代码:

db/seeds.rb

Dog.destroy_all
Admin.destroy_all

Admin.create!(
  email: ENV["ADMIN_EMAIL"],
  password: ENV["ADMIN_PASSWORD"],
  id: 1
)

Dog.create!([
  { 
    location: "92603",
    name: Faker::Name.first_name,
    age: 2,
    title_age: "baby",
    breed: "Corgi" ,
    gender: "female",
    adoptable: true,
    size: "small",
    photo: "http://3milliondogs.com/blog-assets-two/2014/08/corgicute.jpg",
    color: "white, black, orange",
    birth_date: Faker::Date.birthday(1, 9),
    admin_id: 1
  }

models/admin.rb

class Admin < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :dogs
end

models/dog.rb

class Dog < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  include Gravtastic

  gravtastic :secure => true,
             :filetype => :jpg,
             :size => 250

  scope :location, -> (location) { where location: location }
  scope :breed, -> (breed) { where breed: breed }
  scope :title_age, -> (title_age) { where title_age: title_age }
  scope :gender, -> (gender) { where gender: gender }

  validates :name, presence: true
  validates :age, presence: true
  validates :title_age, presence: true
  validates :breed, presence: true
  validates :gender, presence: true
  validates :adoptable, presence: true
  validates :size, presence: true

  belongs_to :admin
end

~/.bash_profile

export ADMIN_EMAIL=gmail@gmail.com
export ADMIN_PASSWORD=password

我已经尝试过..

在seeds.rb中

a = Admin.new(
  email: ENV["ADMIN_EMAIL"],
  password: ENV["ADMIN_PASSWORD"],
  id: 1
)

a.save!(validate: false)

我收到此错误:

ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR:  null value in column "email" violates not-null constraint
DETAIL:  Failing row contains (1, null, , null, null, null, 0, null, null, null, null, 2017-08-13 04:45:12.125121, 2017-08-13 04:45:12.125121, ynNjhGEkh_F3iJKzcvEz, null, 2017-08-13 04:45:12.125384).
: INSERT INTO "admins" ("id", "email", "created_at", "updated_at", "confirmation_token", "confirmation_sent_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"

我也试过没有validate: false,但仍然得到“电子邮件和密码不能为空”。

【问题讨论】:

    标签: ruby-on-rails heroku environment-variables


    【解决方案1】:

    对于 heroku 本地 ENV 变量,您应该使用 config:set, probably this link can help you 进行设置

    heroku config:set ADMIN_EMAIL=xxx@xxx.com
    heroku config:set ADMIN_PASSWORD=xyz
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-08
      • 2020-08-09
      • 1970-01-01
      • 2014-03-16
      • 2018-03-25
      • 1970-01-01
      • 2021-01-24
      • 2015-09-18
      相关资源
      最近更新 更多