简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM