Hi there, I am trying to add some validation to protect against blank fields, but it does not seem to want to work, here is how I am trying to do it: *view* <%= form_tag :action => ''update'' %> for opened *controller* def update if User.update(session[:user_id],params[:user]) flash[:message] = ''User was successfully updated.'' redirect_to :action => ''index'' else flash[:message] = ''An error occurred!'' render :action => ''edit'' end end *model* validates_presence_of :username, :password, :firstname, :lastname, :email, :address1, :postcode, :city, :country, :on => :create validates_presence_of :firstname, :lastname, :email, :address1, :postcode, :city, :country, :on => :update but it updates the form successfully even with blanks! Am I missing something? Cheers guys, Mick -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mick wrote:> Hi there, > > I am trying to add some validation to protect against blank fields, but > it does not seem to want to work, here is how I am trying to do it: > def update > if User.update(session[:user_id],params[:user]) > flash[:message] = ''User was successfully updated.'' > redirect_to :action => ''index'' > else > flash[:message] = ''An error occurred!'' > render :action => ''edit'' > end > end > > but it updates the form successfully even with blanks!From http://caboo.se/doc/classes/ActiveRecord/Base.html#M006410 "If the save fails under validations, the unsaved object is still returned." So the update method does not let you know if the validations passed. Either way your model object is returned, and interpreted as true by the if. This should fix it for you: if User.find(session[:id]).update_attributes(params[:user]) ... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alex Wayne wrote:> Mick wrote: >> Hi there, >> >> I am trying to add some validation to protect against blank fields, but >> it does not seem to want to work, here is how I am trying to do it: >> def update >> if User.update(session[:user_id],params[:user]) >> flash[:message] = ''User was successfully updated.'' >> redirect_to :action => ''index'' >> else >> flash[:message] = ''An error occurred!'' >> render :action => ''edit'' >> end >> end >> >> but it updates the form successfully even with blanks! > > From http://caboo.se/doc/classes/ActiveRecord/Base.html#M006410 > > "If the save fails under validations, the unsaved object is still > returned." > > So the update method does not let you know if the validations passed. > Either way your model object is returned, and interpreted as true by the > if. > > This should fix it for you: > > if User.find(session[:id]).update_attributes(params[:user]) > ...Hi, thanks for that it worked! However, the errors object (where the error messages that result from validates_presence_of) seems to be empty when accessed from the view? Any idea why this is getting lost? The following returns nothing: error_messages_for ''user'' cheers, Mick -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m having a similar issue. Any one have any ideas? -- Nathan Garza AshLeaf Media | Director of Technology Innovations _________________________________________________________ www.ashleafmedia.com | nathan-8gggxoBvU1PsBN0MCq728g@public.gmane.org | 832.514.5726 On 4/10/07, Mick <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Alex Wayne wrote: > > Mick wrote: > >> Hi there, > >> > >> I am trying to add some validation to protect against blank fields, but > >> it does not seem to want to work, here is how I am trying to do it: > >> def update > >> if User.update(session[:user_id],params[:user]) > >> flash[:message] = ''User was successfully updated.'' > >> redirect_to :action => ''index'' > >> else > >> flash[:message] = ''An error occurred!'' > >> render :action => ''edit'' > >> end > >> end > >> > >> but it updates the form successfully even with blanks! > > > > From http://caboo.se/doc/classes/ActiveRecord/Base.html#M006410 > > > > "If the save fails under validations, the unsaved object is still > > returned." > > > > So the update method does not let you know if the validations passed. > > Either way your model object is returned, and interpreted as true by the > > if. > > > > This should fix it for you: > > > > if User.find(session[:id]).update_attributes(params[:user]) > > ... > > Hi, thanks for that it worked! However, the errors object (where the > error messages that result from validates_presence_of) seems to be empty > when accessed from the view? Any idea why this is getting lost? > > The following returns nothing: > > error_messages_for ''user'' > > cheers, > Mick > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You haven''t set an instance variable named @user. Try this instead: def update @user = User.find_by_id(session[:user_id]) @user.attributes = params[:user] flash[:message] = "User was successfully updated" and redirect_to :action => :index and return if @user.save #If we didn''t return, we couldn''t save, so execute this alternate flash and render flash[:message] = "Unable to save changes" render :action => :edit end Alternatively, you could gang this action into the edit action with if request.post? Niels On May 6, 2007, at 12:50 AM, Nathan Garza wrote:> I''m having a similar issue. Any one have any ideas? > > -- > > Nathan Garza > > AshLeaf Media | Director of Technology Innovations > _________________________________________________________ > www.ashleafmedia.com | nathan-8gggxoBvU1PsBN0MCq728g@public.gmane.org | 832.514.5726 > > On 4/10/07, Mick <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > Alex Wayne wrote: > > Mick wrote: > >> Hi there, > >> > >> I am trying to add some validation to protect against blank > fields, but > >> it does not seem to want to work, here is how I am trying to do it: > >> def update > >> if User.update(session[:user_id],params[:user]) > >> flash[:message] = ''User was successfully updated.'' > >> redirect_to :action => ''index'' > >> else > >> flash[:message] = ''An error occurred!'' > >> render :action => ''edit'' > >> end > >> end > >> > >> but it updates the form successfully even with blanks! > > > > From http://caboo.se/doc/classes/ActiveRecord/Base.html#M006410 > > > > "If the save fails under validations, the unsaved object is still > > returned." > > > > So the update method does not let you know if the validations > passed. > > Either way your model object is returned, and interpreted as true > by the > > if. > > > > This should fix it for you: > > > > if User.find(session[:id]).update_attributes(params[:user]) > > ... > > Hi, thanks for that it worked! However, the errors object (where the > error messages that result from validates_presence_of) seems to be > empty > when accessed from the view? Any idea why this is getting lost? > > The following returns nothing: > > error_messages_for ''user'' > > cheers, > Mick > > -- > Posted via http://www.ruby-forum.com/. > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---