Ben Lisbakken
2006-Jun-27 19:14 UTC
[Rails] Custom action on Active Record Validation Failure?
Instead of having it highlight the bad fields when you enter in a record that fails validation, is there a way to make it do an action? I''m looking for something like this: validates_uniqueness_of :user_id,:url => {:controller => "headline", :action => "index"} so that on failing the uniqueness check, it would go to the headline controller. -- Posted via http://www.ruby-forum.com/.
Daniel Higginbotham
2006-Jun-27 19:24 UTC
[Rails] Custom action on Active Record Validation Failure?
I believe that if you put something like the following in your controller, it should work: def update @foo = params[:id] @foo.update_attributes(params[:foo] rescue redirect_to :controller => ''headline'', :action => ''index'' end -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Ben Lisbakken Sent: Tuesday, June 27, 2006 9:14 AM To: rails@lists.rubyonrails.org Subject: [Rails] Custom action on Active Record Validation Failure? Instead of having it highlight the bad fields when you enter in a record that fails validation, is there a way to make it do an action? I''m looking for something like this: validates_uniqueness_of :user_id,:url => {:controller => "headline", :action => "index"} so that on failing the uniqueness check, it would go to the headline controller. -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Alex Wayne
2006-Jun-27 19:43 UTC
[Rails] Re: RE: Custom action on Active Record Validation Failure?
Daniel Higginbotham wrote:> I believe that if you put something like the following in your > controller, > it should work: > > def update > @foo = params[:id] > @foo.update_attributes(params[:foo] > rescue > redirect_to :controller => ''headline'', :action => ''index'' > endMore like: def update @foo = Foo.find(params[:id]) @foo.attributes = params[:foo] if @foo.save flash[:notice] = ''You saved it!'' redirect_to :action => ''list'' else flash[:notice] = ''You borked it!'' redirect_to :controller => ''bad_validation'', :action => ''foo'' end end -- Posted via http://www.ruby-forum.com/.
Daniel Higginbotham
2006-Jun-27 20:50 UTC
[Rails] Re: RE: Custom action on Active Record Validation Failure?
Out of curiosity, why did you decide to use if/else instead of rescue? And why''d you use @foo.attributes = params[:foo] if @foo.save instead of if @foo.update_attributes(params[:foo]) which does the same thing? Also, forgot to mention in my original email that redirecting the browser is the controller''s responsibility, not the model''s, which is why the validation methods don''t allow you to specify what URL to go to when validation fails. -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Alex Wayne Sent: Tuesday, June 27, 2006 9:44 AM To: rails@lists.rubyonrails.org Subject: [Rails] Re: RE: Custom action on Active Record Validation Failure? Daniel Higginbotham wrote:> I believe that if you put something like the following in your > controller, > it should work: > > def update > @foo = params[:id] > @foo.update_attributes(params[:foo] > rescue > redirect_to :controller => ''headline'', :action => ''index'' > endMore like: def update @foo = Foo.find(params[:id]) @foo.attributes = params[:foo] if @foo.save flash[:notice] = ''You saved it!'' redirect_to :action => ''list'' else flash[:notice] = ''You borked it!'' redirect_to :controller => ''bad_validation'', :action => ''foo'' end end -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ajay
2006-Jul-06 18:48 UTC
[Rails] RE: Re: RE: Custom action on Active Record Validation Failur
if/else is much, much more performant than exception handling. You can measure this yourself, write a little test program. Also remember that rescue would "skip" any other code in-between the raised exception and the rescue code, which may or may not be a bad thing. update_attributes and save are equivalent, I''d stick with update_attributes def update_attributes(attributes) self.attributes = attributes save end Daniel Higginbotham wrote:> Out of curiosity, why did you decide to use if/else instead of rescue? > And > why''d you use > > @foo.attributes = params[:foo] > if @foo.save > > instead of > > if @foo.update_attributes(params[:foo]) > > which does the same thing? > > > Also, forgot to mention in my original email that redirecting the > browser is > the controller''s responsibility, not the model''s, which is why the > validation methods don''t allow you to specify what URL to go to when > validation fails.-- Posted via http://www.ruby-forum.com/.
Ajay
2006-Jul-06 18:50 UTC
[Rails] RE: Re: RE: Custom action on Active Record Validation Failur
The main missing link in your code is @foo = Foo.find(params[:id]) Ajay wrote:> if/else is much, much more performant than exception handling. You can > measure this yourself, write a little test program. > > Also remember that rescue would "skip" any other code in-between the > raised exception and the rescue code, which may or may not be a bad > thing. > > update_attributes and save are equivalent, I''d stick with > update_attributes > > def update_attributes(attributes) > self.attributes = attributes > save > end >-- Posted via http://www.ruby-forum.com/.
Ajay
2006-Jul-06 18:52 UTC
[Rails] RE: Re: RE: Custom action on Active Record Validation Failur
Sorry I didn''t look at your code very closely, also update_attributes doesn''t throw an exception, so in this case you must use if/else Ajay wrote:> The main missing link in your code is > @foo = Foo.find(params[:id]) > > Ajay wrote: >> if/else is much, much more performant than exception handling. You can >> measure this yourself, write a little test program. >> >> Also remember that rescue would "skip" any other code in-between the >> raised exception and the rescue code, which may or may not be a bad >> thing. >> >> update_attributes and save are equivalent, I''d stick with >> update_attributes >> >> def update_attributes(attributes) >> self.attributes = attributes >> save >> end >>-- Posted via http://www.ruby-forum.com/.