开发者

How to retain validation error messages after reload?

开发者 https://www.devze.com 2023-02-13 19:22 出处:网络
I am running several validations in my foo model. Here is how I am handling the create and update actions:

I am running several validations in my foo model. Here is how I am handling the create and update actions:

if @foo.update_attributes(params[:foo])
  #party time   
else
  @foo.reload #here is the issue
  render :action => "new" #or edit
end

I have to reload the attribute开发者_如何学运维s because I am using the default form values to run unrelated javascript. The problem is that by reloading the attributes, I apparently lose @foo.errors. Is there a way that I can pass them to the new reloaded variable? I would prefer this over using javascript to prevent the bad form submission. Thanks!


The reload method is obviously not for that. You can: 1. define your method for reverting the values 2. save the errors and then re-assign 3. merge attributes to the @foo from freshly loaded instance.

All of these solutions are very hackish and are not recommended. If I were you, I'd rethink the way things should work.

0

精彩评论

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

关注公众号