环境变量|使用Heroku播种时的验证错误

时间:2017-08-13 05:08:28

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

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

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

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

感谢您的时间,如果您需要其他信息,请告知我们。

我的代码:

分贝/ 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
  }

模型/ 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

模型/ 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的情况下尝试过,但仍然会收到“电子邮件和密码不能为空”。

1 个答案:

答案 0 :(得分:1)

对于heroku本地ENV var,您应该使用config:set,probably this link can help you

进行设置
heroku config:set ADMIN_EMAIL=xxx@xxx.com
heroku config:set ADMIN_PASSWORD=xyz