blackangel6291-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-14 18:42 UTC
Problem with submit form
I have the general submit form with info like address, email, name, etc. However I seem to stumbled into a problem, everytime I access that page, rails validates if the fields are empty or not. I have this code .... def new_user # if pressed submit .... @user = User.new(params[:user]) @user.some_bool_option = true if @user.save flash[:notice] = ''User was successfully created.'' redirect_to :action => ''list'' else render :action => ''new_user'' end end What I would like to see if it''s possible to create the user instance ONLY if the user pressed the submit tag. Is this even possible? --~--~---------~--~----~------------~-------~--~----~ 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 10/14/07, blackangel6291-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <blackangel6291-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have the general submit form with info like address, email, name, > etc. > > However I seem to stumbled into a problem, everytime I access that > page, rails validates if the fields are empty or not. I have this > code .... > > def new_user > # if pressed submit .... > @user = User.new(params[:user]) > @user.some_bool_option = true > if @user.save > flash[:notice] = ''User was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new_user'' > end > end > > What I would like to see if it''s possible to create the user instance > ONLY if the user pressed the submit tag. Is this even possible?The usual idiom would be: def new_user @user = User.new if request.post? @user.attributes = params[:user] if @user.save flash[:notice] = ''User was successfully created.'' redirect_to :action => ''list'' return end end end This makes a GET render with a blank form, while a POST will redirect if successful, or render with the user''s values carrying forward (so errors can be corrected). You can use error_messages_for(:user) in the view to show the errors. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the tip, but it does not work. After I input the data in the field and finally press the submit button, it clears ALL data and gives me the validate_presence_off errors. --~--~---------~--~----~------------~-------~--~----~ 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 14 Oct 2007, at 20:52, N/A N/A wrote:> Thanks for the tip, but it does not work. After I input the data in > the field and finally press the submit button, it clears ALL data > and gives me the validate_presence_off errors.Is it actually submitting the data the way you think it is (ie what is in your form ?) 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 -~----------~----~----~----~------~----~------~--~---
<h1>Create Admin Account</h1> <%= error_messages_for ''user'' %> <%= start_form_tag :action => ''new_admin'' %> <p><label for="user_name">Username</label><br/> <%= text_field ''user'', ''username'' %></p> <p><label for="user_password">Password</label><br/> <%= password_field ''user'', ''password'' %></p> <%= submit_tag "Create Account" %> <%= end_form_tag %> This is in my form. It still does the same thing, once I press submit, it clears all fields and reports the verify_presence_of errors. --~--~---------~--~----~------------~-------~--~----~ 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 15 Oct 2007, at 08:37, N/A N/A wrote:> This is in my form. It still does the same thing, once I press > submit, it clears all fields and reports the verify_presence_of > errors.I''d check that the data being submitted is what you think it is. 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 -~----------~----~----~----~------~----~------~--~---
The data is what I think it is ... --~--~---------~--~----~------------~-------~--~----~ 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 15 Oct 2007, at 08:37, N/A N/A wrote:> <h1>Create Admin Account</h1> > <%= error_messages_for ''user'' %> > > <%= start_form_tag :action => ''new_admin'' %> > > <p><label for="user_name">Username</label><br/> > <%= text_field ''user'', ''username'' %></p> > > <p><label for="user_password">Password</label><br/> > <%= password_field ''user'', ''password'' %></p> > > <%= submit_tag "Create Account" %> > > <%= end_form_tag %> > > This is in my form. It still does the same thing, once I press > submit, it clears all fields and reports the verify_presence_of > errors. > >Are you sure this is the right form ? you''re working on your new_user action, but this is a form for new_admin 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 -~----------~----~----~----~------~----~------~--~---
Yes, im sure it''s the right form, I copied and pasted from an older source. But tis the right form. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Found the problem. I forgot to send the params :P My mistake :) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---