Hi all, I have a mail form, which the user has to fill with some mandatory values (name, email, address and so on). In the action I check for the fields being empty and if some of them are in fact empty I redirect them back to the form issuing an error message for which I use a flash. Like this: def mail_form flash[''notice''] = '''' flash[''notice''] << ''Enter a name'' if @params[''name''].empty? flash[''notice''] << ''Enter an email address'' if @params[''email''].empty? if flash[''notice''].empty? ... go and mail stuff else ... go back to the form end end It works as it is, but I want to keep the values the user has already entered so they don''t have to retype them. I can do: def mail_form flash[''name''] = @params[''name''] flash[''email''] = @params[''email''] ... end Is there a more elegant way to copy all the parameters into the flash? Many thanks in advance! Nicky
Nickolay Kolev wrote:> Hi all, > > I have a mail form, which the user has to fill with some mandatory > values (name, email, address and so on). In the action I check for the > fields being empty and if some of them are in fact empty I redirect them > back to the formWhy don''t you do it the other way round: render the form when the vaildation fails, and redirect to another page when it is successful?
> Why don''t you do it the other way round: render the form when the > vaildation fails, and redirect to another page when it is successful?Thats what I do... perhaps I put it wrong. I first render the form, the user fills it in, submits it to the checking action. If the validation fails, they get redirected to the form, else the mail is sent and a ''thank-you'' page is rendered. Nicky
> flash[''params''] = @paramsDuh, should have thought of that. :-) Thanks! Nicky
Nickolay Kolev wrote:>> Why don''t you do it the other way round: render the form when the >> vaildation fails, and redirect to another page when it is successful? > > > Thats what I do... perhaps I put it wrong. I first render the form, the > user fills it in, submits it to the checking action. If the validation > fails, they get redirected to the form,Don''t redirect to the form, just render it directly.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Nickolay Kolev wrote: | Is there a more elegant way to copy all the parameters into the flash? flash[''params''] = @params - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCWUyoSIxC85HZHLMRApyAAJ9+wDofh+z7JNA/Sw4YJn8nBikmkQCdFM/J 6DF0Y9n70defqw2ameRR/5s=QVuK -----END PGP SIGNATURE-----
Nickolay, If you''re going to store the user info in, say, User class, why don''t you create a new User object in the form action and then use the provided form field helpers [1]? That way you can put the validation in the User model ([2], [3]) and don''t have to use flash for the error messages. Here''s an example on how easily you can do the form (see [1] to [3] for explanations): Model: class User < ActiveRecord::Base validates_presence_of :name, :email, :message => "can''t be empty" validates_confirmation_of :password validates_confirmation_of :email_address, :message => "should match confirmation" end View: <%= form_tag :action => "register" %> <%= text_field "user", "email_address" %> <%= text_field "user", "email_address_confirmation" %> <%= password_field "user", "password" %> <%= password_field "user", "password_confirmation" %> Then, in your controller, do something like this (note that the action is submitting to itself, aka "postback"): def register case @request.method when :get # this is the part that renders the form @user = User.new # you need this for text_field et al to work when :post # this is the part where the form is submitted to @user = User.new(@params["user]) if @user.valid? @user.save redirect_to "wherever you want to" else render_action "register" # this will render the same form as # the get part above, with invalid # fields marked end end end Note that even if you don''t want to save the information anywhere, you can still create a class for it and use the valid? method. Just don''t call the save method, and send the mail instead. //jarkko [1] http://rails.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html [2] http://rails.rubyonrails.com/classes/ActiveRecord/Validations.html [3] http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ ClassMethods.html On 10.4.2005, at 15:29, Nickolay Kolev wrote:> Hi all, > > I have a mail form, which the user has to fill with some mandatory > values (name, email, address and so on). In the action I check for the > fields being empty and if some of them are in fact empty I redirect > them back to the form issuing an error message for which I use a > flash. Like this: > > def mail_form > flash[''notice''] = '''' > flash[''notice''] << ''Enter a name'' if @params[''name''].empty? > flash[''notice''] << ''Enter an email address'' if > @params[''email''].empty? > > if flash[''notice''].empty? > ... go and mail stuff > else > ... go back to the form > end > > end > > It works as it is, but I want to keep the values the user has already > entered so they don''t have to retype them. I can do: > > def mail_form > > flash[''name''] = @params[''name''] > flash[''email''] = @params[''email''] > > ... > > end > > Is there a more elegant way to copy all the parameters into the flash? > > Many thanks in advance! > > Nicky > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Apr 11, 2005 6:08 PM, David Morton <mortonda-0/IDydmJJnNeoWH0uzbU5w@public.gmane.org> wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Andreas Schwarz wrote: > | Nickolay Kolev wrote: > |>> Why don''t you do it the other way round: render the form when the > |>> vaildation fails, and redirect to another page when it is successful? > |> > |> > |> Thats what I do... perhaps I put it wrong. I first render the form, > |> the user fills it in, submits it to the checking action. If the > |> validation fails, they get redirected to the form, > | > | Don''t redirect to the form, just render it directly. > > Not necessarily, because you may not have all the rest of the info needed. > > Instead, just put the @params into a flash object. You could take > everything with: > > flash[''params''] = @params > > and then do the redirect. then in the other methods, check for the > existance ov flash[''params''] and take appropriate action. > > You don''t have to put all of @params in the flash, it may just be > @params[''person''] or something.If you *are* putting all of @params in the flash, why are you redirecting in the first place? It''d be easier to just render the view in question wouldn''t it?> - -- > David Morton > Maia Mailguard server side anti-spam/anti-virus solution: > http://www.maiamailguard.com > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.5 (GNU/Linux) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFCWhRYSIxC85HZHLMRAh2jAJ9tDdPjUnQ+gUyNj/i1EfR86N5DrACeOTce > Sh+71NIwozRs+u0vvpvWFnQ> =uyh7 > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Michael Koziarski wrote: | You don''t have to put all of @params in the flash, it may just be | @params[''person''] or something. | |> If you *are* putting all of @params in the flash, why are you |> redirecting in the first place? It''d be easier to just render the |> view in question wouldn''t it? Not really. I have a page that has a complex set of forms, for which several queries have to be performed. The form submission may only be one tiny little part. It''s much easier to pass the params over to the main function to display the page. I don''t want to have the update logic in the same action as the render, as it could get too messy to maintain. - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCWeJ3SIxC85HZHLMRAgGsAJ427kq3aOmFSXzqZfdpSPqK/XoM4gCfURq2 Q3z4BAKxD/luuDlUOoqgexM=evVK -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Andreas Schwarz wrote: | Nickolay Kolev wrote: |>> Why don''t you do it the other way round: render the form when the |>> vaildation fails, and redirect to another page when it is successful? |> |> |> Thats what I do... perhaps I put it wrong. I first render the form, |> the user fills it in, submits it to the checking action. If the |> validation fails, they get redirected to the form, | | Don''t redirect to the form, just render it directly. Not necessarily, because you may not have all the rest of the info needed. Instead, just put the @params into a flash object. You could take everything with: flash[''params''] = @params and then do the redirect. then in the other methods, check for the existance ov flash[''params''] and take appropriate action. You don''t have to put all of @params in the flash, it may just be @params[''person''] or something. - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCWhRYSIxC85HZHLMRAh2jAJ9tDdPjUnQ+gUyNj/i1EfR86N5DrACeOTce Sh+71NIwozRs+u0vvpvWFnQ=uyh7 -----END PGP SIGNATURE-----