使用 Active Admin 进行验证
Validations with Active Admin
我有两个模型,事件和图像:
class Event < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images
validates :date, presence: true
validates :location, presence: true
validates :name, presence: true
end
class Image < ActiveRecord::Base
belongs_to :event
mount_uploader :file, AvatarUploader
validates :file, presence: true
validates :event, presence: true
end
这是我的迁移:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.date :date
t.string :location
t.string :name
t.timestamps
end
end
end
class CreateImages < ActiveRecord::Migration
def change
create_table :images do |t|
t.string :file
t.string :caption
t.boolean :featured_image
t.integer :event_id
t.timestamps
end
end
end
我正在使用 Carrierwave 上传图像。在没有内置验证的情况下,我可以毫无问题地使用此功能,但我试图阻止上传未分配 event_id 的图像。
目前,我的 ActiveAdmin 文件如下所示:
ActiveAdmin.register Event do
menu label: "Events"
permit_params :date, :location, :name, images_attributes: [:id, :file, :caption, :featured_image, :event_id]
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Event Details" do
f.input :name
f.input :location
f.input :date, :start_year => 2000, :end_year => 2020
f.inputs "Images" do
f.has_many :images, :allow_destroy => true, :heading => false, :new_record => true, :html => { :multipart => true } do |p|
p.input :event_id, :value => f.object.id
p.input :file, :as => :file
p.input :caption
p.input :featured_image
end
end
end
f.actions
end
有问题的主线是我将 event_id 分配给对象 ID(事件)的值。
有办法吗?
改变
p.input :event_id, :value => f.object.id
到
p.input :event_id, input_html: { value: f.object.id }
一切就绪
我有两个模型,事件和图像:
class Event < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images
validates :date, presence: true
validates :location, presence: true
validates :name, presence: true
end
class Image < ActiveRecord::Base
belongs_to :event
mount_uploader :file, AvatarUploader
validates :file, presence: true
validates :event, presence: true
end
这是我的迁移:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.date :date
t.string :location
t.string :name
t.timestamps
end
end
end
class CreateImages < ActiveRecord::Migration
def change
create_table :images do |t|
t.string :file
t.string :caption
t.boolean :featured_image
t.integer :event_id
t.timestamps
end
end
end
我正在使用 Carrierwave 上传图像。在没有内置验证的情况下,我可以毫无问题地使用此功能,但我试图阻止上传未分配 event_id 的图像。
目前,我的 ActiveAdmin 文件如下所示:
ActiveAdmin.register Event do
menu label: "Events"
permit_params :date, :location, :name, images_attributes: [:id, :file, :caption, :featured_image, :event_id]
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Event Details" do
f.input :name
f.input :location
f.input :date, :start_year => 2000, :end_year => 2020
f.inputs "Images" do
f.has_many :images, :allow_destroy => true, :heading => false, :new_record => true, :html => { :multipart => true } do |p|
p.input :event_id, :value => f.object.id
p.input :file, :as => :file
p.input :caption
p.input :featured_image
end
end
end
f.actions
end
有问题的主线是我将 event_id 分配给对象 ID(事件)的值。
有办法吗?
改变
p.input :event_id, :value => f.object.id
到
p.input :event_id, input_html: { value: f.object.id }
一切就绪