为什么我的控制器未将对象保存在数据库中?

时间:2018-07-17 22:33:29

标签: ruby-on-rails model controller console ruby-on-rails-5

我正在为我的模型保存CalendarEntry的输入对象,但是在视图中由于某种原因单击“完成”时,该对象没有保存。

从我的角度来看,我的控制器很好,但是也许问题出在这里:

  

控制器

def create
  @entry = CalendarEntry.new(entries_params)
  binding.pry
  if @entry.save
    render 'admins/calendar_entries/index'
  else
    render 'admins/calendar_entries/new'
  end
end

def entries_params
  conversions
  params.require(:calendar_entry).permit(:entry_type, :entry_level, :visible, :title, :publication_date, :expiration_date, :content, :phonenumber, :website, :state, :city, :address)
end

def conversions
  params[:calendar_entry][:entry_type] = params[:calendar_entry][:entry_type].to_i
  params[:calendar_entry][:entry_level] = params[:calendar_entry][:entry_level].to_i
end
  

控制台

enter image description here

正如您在控制台中看到的那样,我在问我两个值“ calendar_categories”和“ calendar_entry_categories”,但由于我的“ CalendarEntry”只在其中询问值,所以应该问这个问题,

P.D。 id,created_at和updated_at是自动生成的。

  

更新7月17日-晚上11:12

此处定义的模式:

create_table "calendar_categories", force: :cascade do |t|
    t.string "name"
    t.string "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "calendar_entries", force: :cascade do |t|
    t.integer "entry_type"
    t.integer "entry_level"
    t.boolean "visible"
    t.string "title"
    t.datetime "publication_date"
    t.datetime "expiration_date"
    t.text "content"
    t.string "phonenumber"
    t.string "website"
    t.string "state"
    t.string "city"
    t.string "address"
    t.string "profile_picture"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "calendar_entry_categories", force: :cascade do |t|
    t.bigint "calendar_entry_id"
    t.bigint "calendar_category_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["calendar_category_id"], name: "index_calendar_entry_categories_on_calendar_category_id"
    t.index ["calendar_entry_id"], name: "index_calendar_entry_categories_on_calendar_entry_id"
  end

此处定义的模型:

class CalendarEntry < ApplicationRecord
 scope :visible, -> { where(visible: true) }
 scope :invisible, -> { where(visible: false) }
 scope :expired, -> { where('expiration_date < ?', Time.zone.now) }
 scope :active, -> { where('expiration_date >= ?', Time.zone.now) }

 has_many :calendar_entry_categories, dependent: :destroy
 has_many :calendar_categories, through: :calendar_entry_categories

 enum entry_type: %i[event program]
 enum entry_level: %i[municipal statal federal injuve]

 mount_uploader :profile_picture, CalendarEntryProfilePictureUploader

 validates :entry_type, :entry_level, :visible, :title,
        :expiration_date, :content, :phonenumber, :website, :state, :city,
        :address, :calendar_categories,
        :calendar_entry_categories, presence: true
 validates :publication_date, presence: true, on: :update
 validates :title, uniqueness: true
 validates :phonenumber, numericality: { only_integer: true }
 validates :phonenumber, length: { is: 10 }
 validates_inclusion_of :entry_type, in: CalendarEntry.entry_types
 validates_inclusion_of :entry_level, in: CalendarEntry.entry_levels
 validate :expiration_date_range

 before_validation :init, on: :create

 private

 def init
   self.publication_date ||= Time.zone.now
 end

 def expiration_date_range
   return if !expiration_date.nil? && expiration_date > publication_date
   errors.add(:expiration_date, :past_expiration_date)
 end
end

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试在模型验证中验证calendar_categoriescalendar_entry_categories的存在。

考虑到CalendarEntryCategoryCalendarEntry存在之前就不存在,并且CalendarCategoryCalendarEntry时可能并不总是存在,因此您将无法验证它们的存在。已创建。

因此,要使其正常工作,您要做的就是删除

在您的:calendar_categories, :calendar_entry_categories模型中的presence: true验证中

CalendarEntry