Here is an example that seems perfect for Ajax that I have not seen implemented nor can figure out how to do it. I am hoping someone can give me some pointers. On the sign-up page, I would like to do my validations (password length, username uniqueness,etc ) Ajax-style. But if all validates, then redirect to the success page. The combining of redirection to a new page with Ajax is throwing me. Steve
Hi, I''m assuming you have the Ajax validation working, if not, the wiki (http://wiki.script.aculo.us/scriptaculous/show/HomePage) should have some examples for you... A high-level view of what i think you want to do is: Attach some javascript to the submit button (using onsubmit) When the button is pressed, do your ajax validation If authentication failed - Update your screen as required and return false - this causes the submit to stop. If authentication passes - Use the javascript document.location method to redirect the browser to somewhere else,. You could also return true to submit the form, but that would mean you authenticate twice. I''d err on the side of caution with using ajax for something fundamental and provide a non-javascript method of logging in to your system. Hope that helps, reply to list if it doesn''t! Cheers, Rob. On 8/8/05, Steve Odom <steve.odom@gmail.com> wrote:> Here is an example that seems perfect for Ajax that I have not seen > implemented nor can figure out how to do it. I am hoping someone can > give me some pointers. > > On the sign-up page, I would like to do my validations (password > length, username uniqueness,etc ) Ajax-style. But if all validates, > then redirect to the success page. The combining of redirection to a > new page with Ajax is throwing me. > > Steve > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >-- Rob Sharp email/jabber: rob@sharp.id.au web: http://sharp.id.au pgp: 0E2C C63B BA04 DEB4 7CC0 84FD 17E3 6AA4 87FB 62DF
On 08/08/05, Steve Odom <steve.odom@gmail.com> wrote:> But if all validates, > then redirect to the success page.Don''t do this. This comes unexpected to the user. Don''t take away their ability to review the data and give the final "go" signal. They may want to change the username, etc., after all. User signup is a irreversible action. Think "reviewing the data of a money transfer". You wouldn''t want automatic submit on that, would you? Technically, you would just submit() the form. Bye, Martin
On 8/8/05, Martin Bialasinski <klingeling@gmail.com> wrote:> On 08/08/05, Steve Odom <steve.odom@gmail.com> wrote: > > > But if all validates, > > then redirect to the success page. > > Don''t do this. This comes unexpected to the user. Don''t take away > their ability to review the data and give the final "go" signal. They > may want to change the username, etc., after all. > > User signup is a irreversible action. Think "reviewing the data of a > money transfer". You wouldn''t want automatic submit on that, would > you? > > Technically, you would just submit() the form.I see your point, but surely the user hitting the submit button is enough to suggest that the user wishes to perform the action? In a non-ajax situation, the actions and outcome would be the same... If a second validation is required, this can be handled in addition to the scenario above. Rob. -- Rob Sharp email/jabber: rob@sharp.id.au web: http://sharp.id.au pgp: 0E2C C63B BA04 DEB4 7CC0 84FD 17E3 6AA4 87FB 62DF
On 08/08/05, Rob Sharp <rob.sharp@gmail.com> wrote:> On 8/8/05, Martin Bialasinski <klingeling@gmail.com> wrote:> > Don''t do this. This comes unexpected to the user. Don''t take away > > their ability to review the data and give the final "go" signal. They > > may want to change the username, etc., after all.> I see your point, but surely the user hitting the submit button is > enough to suggest that the user wishes to perform the action?Sure it is, this is the way forms work and the user expect them to work.> In a > non-ajax situation, the actions and outcome would be the same...I can''t follow you on that. Can you elaborate?> If a > second validation is required, this can be handled in addition to the > scenario above.Any validation done by AJAX means has to be repeated server-side anyway, if this is what you mean. The workflow is this: 1. User inputs data. 2. AJAX used to provide validation. 3. User hits submit. 4. ALL data is validated server-side. 5. On error: Show the form again and mark the erronous data 6. On success: Do some stuff and show output/redirect user The only step AJAX adds is no. 2. And the good thing is, this step is optional, so it works, even if the browser doesn''t support Ajax / has Scripting/ActiveX off, etc. A different thing is, if AJAX is used as a mean to gether data for the form with no fallback. Then it is not optional, of cause. Bye, Martin
1 through 6 perfectly describes ASP.NET''s validation framework. Now if only Rails had a cleaner split between Page validators and Model validators... --- Martin Bialasinski <klingeling@gmail.com> wrote:> On 08/08/05, Rob Sharp <rob.sharp@gmail.com> wrote: > > On 8/8/05, Martin Bialasinski > <klingeling@gmail.com> wrote: > > > > Don''t do this. This comes unexpected to the > user. Don''t take away > > > their ability to review the data and give the > final "go" signal. They > > > may want to change the username, etc., after > all. > > > I see your point, but surely the user hitting the > submit button is > > enough to suggest that the user wishes to perform > the action? > > Sure it is, this is the way forms work and the user > expect them to work. > > > In a > > non-ajax situation, the actions and outcome would > be the same... > > I can''t follow you on that. Can you elaborate? > > > If a > > second validation is required, this can be handled > in addition to the > > scenario above. > > Any validation done by AJAX means has to be repeated > server-side > anyway, if this is what you mean. > > The workflow is this: > > 1. User inputs data. > 2. AJAX used to provide validation. > 3. User hits submit. > 4. ALL data is validated server-side. > > 5. On error: Show the form again and mark the > erronous data > 6. On success: Do some stuff and show > output/redirect user > > The only step AJAX adds is no. 2. And the good thing > is, this step is > optional, so it works, even if the browser doesn''t > support Ajax / has > Scripting/ActiveX off, etc. > > A different thing is, if AJAX is used as a mean to > gether data for the > form with no fallback. Then it is not optional, of > cause. > > Bye, > Martin > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs>____________________________________________________ Start your day with Yahoo! - make it your home page http://www.yahoo.com/r/hs
Thanks for the tips. I''ve got it working - but not perfectly. There are a couple of problems. 1. my highlight effect for errors is not working. 2. my specific validation errors don''t show up; only my "Sorry, an error has occurred". Tips and improvements are welcome. Here is my signup view code: <h1>Signup</h1> <%= form_remote_tag(:update => "message", :url => { :action => ''signup'' }, :loading => "Element.show(''search-indicator'')", :complete => visual_effect(:highlight, "message", :duration => 3.0 ))%> <label for="user_login">Desired login:</label><br/> <%= text_field "user", "login", :size => 30 %><br/> <label for="user_password">Choose password:</label><br/> <%= password_field "user", "password", :size => 30 %><br/> <label for="user_password_confirmation">Confirm password:</label><br/> <%= password_field "user", "password_confirmation", :size => 30 %><br/> <label for="user_email">Email Address:</label><br/> <%= text_field "user", "email", :size => 50 %><br/> <%= submit_tag "Submit" %> <%= end_form_tag %> <div id="message"> <%= image_tag("indicator.gif", :id => ''search-indicator'', :style => ''display:none'') %> <%= render_errors @user %> <%= @flash.now[''notice''] if @flash[''notice''] %> </div> --------------->Here is my action def signup @user = User.new(@params[:user]) if @request.post? if @user.save @session[:user] = User.authenticate(@user.login, @params[:user][:password]) addcash(@session[:user].id) render_text "<script>document.location = ''/portfolio'';</script>" else render_text "Sorry, an error occurred." end end end Steve
Thanks, San. I tried your suggestion of changing the render_text "some error message" to render :action => ''signup'' but what that does is since, in my form_remote_tag, I am instructing the response to update my div, the entire page gets re-rendered in that one div. So everything on my page is duplicated. But you are right, the validation errors do show up on the newly rendered half. Besides, rerendering the page seems to defeat the reason for using Ajax in the first place. I figured out the effect problem - I upgraded prototype.js to a beta version (1.4) to use with scriptaculous and that seemed to break my effects. I still can''t get the validations to show up. Steve . On 8/9/05, sanzbox@yahoo.com <sanzbox@yahoo.com> wrote:> Hi Steve, > > Try changing the ''render_text'' to ''render :action => > ''signup'''' or whatever the name of your signup method > is. > > Rails needs to re-render the page for you to see > validation errors. > > ''render_text'' (or the new render :text => ''text goes > here'') will bypass the .rhtml template and only send > the text you specify. That''s why you''re only seeing > your own error message. > > -San > > --- Steve Odom <steve.odom@gmail.com> wrote: > > > Thanks for the tips. I''ve got it working - but not > > perfectly. There > > are a couple of problems. > > > > 1. my highlight effect for errors is not working. > > 2. my specific validation errors don''t show up; only > > my "Sorry, an > > error has occurred". > > > > Tips and improvements are welcome. > > > > Here is my signup view code: > > > > <h1>Signup</h1> > > > > <%= form_remote_tag(:update => "message", > > :url => { :action => ''signup'' }, > > :loading => > > "Element.show(''search-indicator'')", > > :complete => visual_effect(:highlight, > > "message", :duration => 3.0 ))%> > > > > > > > > <label for="user_login">Desired > > login:</label><br/> > > <%= text_field "user", "login", :size => 30 > > %><br/> > > <label for="user_password">Choose > > password:</label><br/> > > <%= password_field "user", "password", :size => > > 30 %><br/> > > <label for="user_password_confirmation">Confirm > > password:</label><br/> > > <%= password_field "user", > > "password_confirmation", :size => 30 %><br/> > > <label for="user_email">Email > > Address:</label><br/> > > <%= text_field "user", "email", :size => 50 > > %><br/> > > > > <%= submit_tag "Submit" %> > > <%= end_form_tag %> > > <div id="message"> > > <%= image_tag("indicator.gif", > > :id => ''search-indicator'', > > :style => ''display:none'') %> > > <%= render_errors @user %> > > <%= @flash.now[''notice''] if @flash[''notice''] %> > > </div> > > > > --------------->Here is my action > > def signup > > @user = User.new(@params[:user]) > > > > if @request.post? > > if @user.save > > @session[:user] > > User.authenticate(@user.login, > > @params[:user][:password]) > > addcash(@session[:user].id) > > render_text "<script>document.location > > ''/portfolio'';</script>" > > else > > render_text "Sorry, an error occurred." > > end > > end > > end > > > > Steve > > _______________________________________________ > > Rails-spinoffs mailing list > > Rails-spinoffs@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > > > > __________________________________ > Yahoo! Mail > Stay connected, organized, and protected. Take the tour: > http://tour.mail.yahoo.com/mailtour.html > >
For those following at home, if i comment out the ''render :text => "Sorry, an error occurred" in my signup action like thus.... def signup @user = User.new(@params[:user]) if @request.post? if @user.save @session[:user] = User.authenticate(@user.login, @params[:user][:password]) addcash(@session[:user].id) render :text => "<script>document.location = ''/portfolio'';</script>" else #render :text => "Sorry, an error occurred." end end end ...then my validations will appear on my signup view with Ajax. However - a major however - the whole page gets rerendered in the div. So my solution was to replace it with this: else #render :text => "Sorry, an error occurred." render :inline => "<%= error_messages_for ''user'' %>" end