开发者

Rails - How do I catch and return a warning when the edited values are not the same

开发者 https://www.devze.com 2023-03-07 07:04 出处:网络
How do I catch and return a warning when the edited values are not the same causing rails to not开发者_运维百科 perform an UPDATE?You could handle it in your controller. In a basic update definition :

How do I catch and return a warning when the edited values are not the same causing rails to not开发者_运维百科 perform an UPDATE?


You could handle it in your controller. In a basic update definition :

def update
  if birth_date_changed? && @user.update_attributes(params[:user])
    flash[:notice] = 'User was successfully updated.'
    redirect_to post_url(@post)
  else
    flash[:warning] = 'Birth date did not changed' unless birth_date_changed?
    render :action => "edit"
  end
end

private
def birth_date_changed?
  @user.birth_date != params[:user][:birth_date]
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号