我开发了一个应用程序,允许登录用户(厨师)为食谱添加评论,我在开发中首先测试它并且运行良好,但是当我将它部署到Heroku时,应用程序无法正常工作,所以我跑了在我的终端' heroku运行rails console'检查发生了什么,所以我得到以下内容:
Set IE = GetIE("www.google.com")
以上所有操作都很完美,但是当我输入@ comment.save时,它会失败:
@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: "wamba@goths.com", 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: "wamba@goths.com", 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>
所以,我检查了我的@recipe和@chef是否有效,@ chef是否有效,但是@recipe无效,但因为@comment无效。所以我检查了@comment
@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
我想知道是什么:文章为什么我需要验证一个不存在的属性?我检查了schema.rb并运行了&#39; Heroku pg:psql&#39;而且我无法找到有关:article属性的信息。我确定:文章不在我的模特中。
@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
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
你知道到底发生了什么吗?
答案 0 :(得分:0)
由于该错误仅出现在制作中,我删除了生产中的文章验证(自动生成),用于评论。 class_eval。
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)}
if Rails.env.production?
Comment.class_eval do
_validators[:article]
.find { |v| v.is_a? ActiveRecord::Validations::PresenceValidator }
.attributes
.delete(:article)
end
end
end
无论如何,我认为这是一个铁轨错误,感谢那些试图帮助我的人。