[英]How to setup controller to respond to a specific object rails
I am trying to set up my controller so it responds to the "first" video record in the database so it will skip the validations in the model. 我正在尝试设置控制器,使其响应数据库中的“第一个”视频记录,因此它将跳过模型中的验证。
video.rb
validates :link, presence: true, format: YT_LINK_FORMAT, unless: :skip_video_validation
before_create :uid_link_match, unless: :skip_video_validation
before_update :uid_link_match, unless: :skip_video_validation
attr_accessor :skip_video_validation
end
How to set up in controller so that I can get first video to skip these validations. 如何在控制器中进行设置,以便获得第一个视频以跳过这些验证。
def update
@video = Video.find(params[:id])
if @video == @video[0]
@video.skip_video_validation = true
if @video.update_attributes(video_params)
flash[:success] = 'Video updated!'
redirect_to root_url
else
render 'edit'
end
else
...
end
Could you please explain what I am doing wrong and why. 您能否解释我做错了什么以及为什么。
Full code should be in controller 完整代码应在控制器中
def update
@user = User.find(params[:user_id])
@video = Video.find(params[:id])
if @video == @user.videos[0]
@video.skip_video_validation = true
if @video.update_attributes(video_params)
flash[:success] = 'Video updated!'
redirect_to root_url
else
render 'edit'
end
else
...
end 结束
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.