I have a activeadmin form to which allows to add a youtube URL. I validate this URL in my video model. It does works well when I want to add a new video but does nothing when I'm editing the video.
app/admin/video.rb :
ActiveAdmin.register Media, as: 'Videos' do
form do |f|
f.inputs "Add youtube video" do
f.input :category
f.semantic_errors :error
f.has_many :videos, allow_destroy: true do |g|
g.input :mylink, :label => "Youtube link : ", :type => :text
end
actions
end
end
end
model/video.rb :
class Video < ApplicationRecord
belongs_to :media
attr_accessor :mylink
YT_LINK_FORMAT = /\A.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*\z/i
before_create :before_add_to_galerie
before_update :before_add_to_galerie
def before_add_to_galerie
uid = @mylink.match(YT_LINK_FORMAT)
self.uid = uid[2] if uid && uid[2]
if self.uid.to_s.length != 11
self.errors.add(:mylink, 'is invalid.')
throw :abort
false
elsif Video.where(uid: self.uid).any?
self.errors.add(:mylink, 'is not unique.')
throw :abort
false
end
end
validates :mylink, presence: true, format: YT_LINK_FORMAT
end
It looks like the before_update method is never triggered.
How can I make my edit work ?
EDIT :I figured out that it is calling the before_update link for the Media model and not for the Video one.But the create is fine.