Hi, I want to do a simple thing: Validate a form so it raises an error when a field is empty. The thing is that I don''t save the contents of that form into DB. What I''ve done is: 1- Add model like this: class Password < ActiveRecord::Base validates_presence_of :email, :on => :create end 2- The view is: <%= error_messages_for :password %> <% form_for :password, :url => { :action => ''create'' } do |f| %> What was the email address used to create your account?<br/><br/> <%= f.text_field :email, :size => "50" %> <%= submit_tag ''Reset Password'' %> <% end %> 3- The controller is: ... def create @user = User.find_for_forget(params[:email]) return unless request.post? if @user = User.find_for_forget(params[:email]) @user.forgot_password @user.save redirect_to login_path else render :action => ''new'' end end 4- And my problem is that I get no errors (from the system) when I press the button in the form. Shouldn''t it validate it when the create method is called? I didn''t generate the model I just added the password.rb to use the validate utilities. Again, I just want to see the error that rails has by default. Thanks for any hints you might have. -- 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 -~----------~----~----~----~------~----~------~--~---
> > <%= error_messages_for :password %>there is no password object in your create action, maybe this should be <%= error_messages_for :user %>> def create > @user = User.find_for_forget(params[:email]) > return unless request.post? > if @user = User.find_for_forget(params[:email]) > @user.forgot_password > @user.save > redirect_to login_path > else > render :action => ''new'' > end > end > >can''t see, what this has to do with the password. And why do you search for the user twice? Anyway, you can check validations with valid? this runs validate and validate_on_create or validate_on_update and returns true if no errors were added otherwise false. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi, <%= error_messages_for :user %> didn''t help 8-(> can''t see, what this has to do with the password. And why do you search > for the user twice?Yep I search twice because I forgot to remove it from previous test, sorry. This is from the password controller, it sets some flags on the user object. But in practice takes care of triggering some email sending to users when they want to reset their password.> Anyway, you can check validations with valid?I was checking that but the problem is that I don''t know on which object run the valid? method. Cheers. -- 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 -~----------~----~----~----~------~----~------~--~---
On 23 May 2008, at 13:13, comopasta Gr wrote:> > Hi, > > I want to do a simple thing: Validate a form so it raises an error > when > a field is empty. The thing is that I don''t save the contents of that > form into DB. > > What I''ve done is: > > 1- Add model like this: > > class Password < ActiveRecord::Base > validates_presence_of :email, :on => :create > endThis means ''run the validation when the object is created''. It has absolutely nothing to do with the fact that this might happen from a controller action called create (the model doesn''t care in the least about this). Fundamentally though, you don''t validate forms, you validate activerecord objects. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, Then I either: 1- Create my own div with the error messages if the field is empty 2- Learn how to trigger one of those system errors that the validations use Any pointers for #2 are welcome. But that explains a lot, thanks. I knew that not really creating any records could be an issue but I thought that on :create was directly related to the controller function. It doesn''t as you mentioned. Regards. -- 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 -~----------~----~----~----~------~----~------~--~---