0
我開發了一個應用程序,允許登錄用戶(廚師)爲食譜添加評論,我在開發中首先對其進行了測試,並且運行良好,但是當我將它部署到Heroku時,該應用程序無法正常工作,所以我跑到了終端「Heroku的運行軌道控制檯」來檢查發生什麼,所以我得到以下:以上爲什麼我的Rails應用程序在Heroku上生成不需要的屬性?
@recipes = Recipe.first
=> #<Recipe id: 1, name: "RecipeOne", description: "Potato\r\nSausage",
created_at: "2017-09-16 00:58:19", updated_at: "2017-09-16 00:58:19", chef_id:
1>
@comment = @recipe.comments.build(description:'a delicious recipe')
=> #<Comment id: nil, description: "a delicious recipe", chef_id: nil,
recipe_id: 1, created_at: nil, updated_at: nil>
@chef = Chef.last
D, [2017-09-16T16:02:27.645527 #4] DEBUG -- : Chef Load (1.9ms) SELECT
"chefs".* FROM "chefs" ORDER BY "chefs"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Chef id: 2, name: "Wamba", email: "[email protected]", created_at: "2017-
09-16 01:13:36", updated_at: "2017-09-16 01:13:36", password_digest:
"$2a$10$WiIVsMk25EurnDaByNDNH.hWONivodBcvP8.cQJk8cM...", admin: false>
@comment.chef = @chef
=> #<Chef id: 2, name: "Wamba", email: "[email protected]", created_at: "2017-
09-16 01:13:36", updated_at: "2017-09-16 01:13:36", password_digest:
"$2a$6786544310$WiIVsMk25EurnDaByNDNH67428.hWONiv8900odBcvP8.cQJk8cM...", admin: false>
完美的作品,但是當我鍵入@ comment.save,它失敗:
@comment.save
D, [2017-09-16T16:04:22.305141 #4] DEBUG -- : (7.4ms) BEGIN
D, [2017-09-16T16:04:22.354525 #4] DEBUG -- : (9.5ms) ROLLBACK
=> false
所以,我檢查了我的@recipe和@chef是有效的,@chef是有效的,但@recipe無效,但是因爲@comment無效。所以我檢查@評論
@comment.errors
=> #<ActiveModel::Errors:0x0000000288d770 @base=#<Comment id: nil,
description: "a delicious recipe", chef_id: 2, recipe_id: 1, created_at: nil,
updated_at: nil>, @messages={:article=>["must exist"]}, @details={:article=>
[{:error=>:blank}]}>
我想知道什麼是文章和爲什麼我需要驗證一個不存在的屬性?我檢查了schema.rb,並且運行了'Heroku pg:psql',我找不到關於:article屬性的信息。我敢肯定:文章不在我的模特中。
#schema.rb
ActiveRecord::Schema.define(version: 20170912043409) do
create_table "chefs", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.boolean "admin", default: false
end
create_table "comments", force: :cascade do |t|
t.text "description"
t.integer "chef_id"
t.integer "recipe_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "ingredients", force: :cascade do |t|
t.string "name"
end
create_table "recipe_ingredients", force: :cascade do |t|
t.integer "recipe_id"
t.integer "ingredient_id"
end
create_table "recipes", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "chef_id"
end
end
型號:
#comment.rb
class Comment < ApplicationRecord
validates :description, presence: true, length: {minimum: 4, maximum: 140}
belongs_to :chef
belongs_to :recipe
validates :chef_id, presence: true
validates :recipe_id, presence: true
default_scope -> {order(updated_at: :desc)}
end
#recipe.rb
class Recipe < ApplicationRecord
validates :name, presence: true
validates :description, presence: true, length: {minimum: 5, maximum:500}
belongs_to :chef
validates :chef_id, presence: true
default_scope-> { order(updated_at: :desc) }
has_many :recipe_ingredients
has_many :ingredients, through: :recipe_ingredients
has_many :comments, dependent: :destroy
end
#chef.rb
class Chef < ApplicationRecord
before_save {self.email = email.downcase}
validates :name, presence: true,
length: {maximum: 30}
VALID_EMAIL_REGEX = /\A[\w+-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true ,length:{maximum: 255},
format:{with: VALID_EMAIL_REGEX},
uniqueness:{case_sensitive: false}
has_many :recipes, dependent: :destroy
has_secure_password
validates :password, presence: true, length:{minimum: 5}, allow_nil: true
has_many :comments, dependent: :destroy
end
#recipe_ingredient.rb
class RecipeIngredient< ApplicationRecord
belongs_to :ingredient
belongs_to :recipe
end
#ingredient.rb
class Ingredient <ApplicationRecord
validates :name, presence: true, length: {minimum: 3, maximum: 25}
validates_uniqueness_of :name
has_many :recipe_ingredients
has_many :recipes, through: :recipe_ingredients
end
rails -v => Rails 5.1.3
ruby -v => ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin15]
你有什麼真正發生的任何想法?
驗證錯誤來自模型,而不是模式。也許你可以展示模型。 –
好的,我添加了模型。 –
好的,那麼你可以嘗試添加此驗證評論模型? '驗證:文章,存在:false'或'validates:article_id,presence:false' –