ruby on rails - Validations with Active Admin -


i have 2 models, event , image:

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 

here migrations:

class createevents < activerecord::migration   def change     create_table :events |t|       t.date :date       t.string :location       t.string :name        t.timestamps   end  end end   class createimages < activerecord::migration   def change     create_table :images |t|       t.string :file       t.string :caption       t.boolean :featured_image       t.integer :event_id        t.timestamps   end  end end 

i using carrierwave upload image. have no problem getting feature work when no validations built in, i'm trying prevent image being uploaded it's event_id not being assigned.

currently, activeadmin file looks this:

activeadmin.register event   menu label: "events"    permit_params :date, :location, :name, images_attributes: [:id, :file, :caption, :featured_image, :event_id]    form :html => { :enctype => "multipart/form-data" } |f|     f.inputs "event details"       f.input :name       f.input :location       f.input :date, :start_year => 2000, :end_year => 2020      f.inputs "images"       f.has_many :images, :allow_destroy => true, :heading => false, :new_record => true, :html => { :multipart => true } |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 

the main line in question assigning event_id value of object's id (the event).

is there way this?

change

p.input :event_id, :value => f.object.id 

to

p.input :event_id, input_html: { value: f.object.id } 

and you're set


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -