ruby - Rails 4 ArgumentError in Controller#create -
i'm trying create web application allows users add videos youtube getting argumenterror in videoscontroller#create:
the provided regular expression using multiline anchors (^ or $), may present security risk. did mean use \a , \z, or forgot add :multiline => true option?
videocontroller.rb
class videoscontroller < applicationcontroller def index @videos = video.order('created_at desc') end def new @video = video.new end def create @video = video.new(video_params) if @video.save flash[:success] = 'video added!' redirect_to root_url else render 'new' end end def video_params params.require(:video).permit(:link) end end videos.rb
class video < activerecord::base yt_link_format = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/i before_create -> uid = link.match(yt_link_format) self.uid = uid[2] if uid && uid[2] if self.uid.to_s.length != 11 self.errors.add(:link, 'is invalid.') false elsif video.where(uid: self.uid).any? self.errors.add(:link, 'is not unique.') false else get_additional_info end end validates :link, presence: true, format: yt_link_format private do add \a , \z yt_link_format line?
in videos.rb change definition of yt_link_format:
yt_link_format = /\a.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*\z/i (basically replacing initial ^ \a, , putting \z @ end)
\a , \z match beginning , end of string; ^ , $ match beginning , end of line. since not defining regex multiline need use string matchers not line ones.
it's error trying tell you; it's bit better described in api documentation here: http://apidock.com/rails/activemodel/validations/helpermethods/validates_format_of
Comments
Post a Comment