Hi! If i have an object it''s easy. But i''m making a simple contact form and i check if email address is correct. If it''s not i redisplay the form with the info that there was an error (should i use redirect_to or render :action? what''s the difference?), but all previously submitted data is lost. I tried creating variables using names of the form fields, but it didn''t work. -- Posted via http://www.ruby-forum.com/.
Chris Hall
2006-Mar-23 17:35 UTC
[Rails] How to redisplay submitted data on validation error?
render :action overrides the default render for the current action with the render of another action. redirect_to effectively sends a header to the browser telling it to load a new URL (controller/action) specified in the redirect_to call. you won''t have any access to the data that was used in the action unless its in the session. to explain what is happening below, when you go to the new action, rails creates an empty model. that model''s data is then used to populate the form in the view. since it''s an ''empty'' model, the fields are empty. the user then fills out the form and submits to the create action. the create action creates a model from the submitted data and attempts to save it. if the save is successful, the browser is redirected to the list action. if the save is unsuccessful, the view for the new action is rendered. in this case, we have a model instance loaded with the submitted data, so when the new form is displayed, the data from the model is used to populate the form. the form helpers are smart and know if a model has errors and will automatically hilight the fields with errors for you (see comments below example). class ExampleController < ApplicationController def new @model = Model.new end def create @model = Model.new(@params[:model] if @model.save redirect_to :action => :list else render_action => :new end end end new.rhtml <%= form_start_tag :action => :create %> <label for="model_email"><%= text_field :model, :email %> <%= submit_tag "Submit" %> <%= form_end_tag %> By using the helper methods this way (ie, text_field as opposed to text_field_tag), Rails will automatically repopulate the fields with the data that was submitted and hilight the fields with errors. you''ll need to either include the scaffold.css file or define .fieldWithErrors yourself for the hilight to work. in addition to this, you can use error_messages_for(:model) to provide a descriptive message as to why the fields have errors. this also relies on some style definitions found in scaffold.css. hope this helps. Chris On 3/23/06, szymek <g0nzo@o2.pl> wrote:> > Hi! > > If i have an object it''s easy. > But i''m making a simple contact form and i check if email address is > correct. If it''s not i redisplay the form with the info that there was > an error (should i use redirect_to or render :action? what''s the > difference?), but all previously submitted data is lost. > I tried creating variables using names of the form fields, but it didn''t > work. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060323/c2a337f4/attachment.html
szymek
2006-Mar-23 17:54 UTC
[Rails] Re: How to redisplay submitted data on validation error?
Thanks! It cleared up few things for me. However if i don''t have any model for my contact form and i''d like to redisplay submitted data, what then? Can submitted data be redisplayed without using a model? Or should i just create a model for it? I''m using the submitted data only to send it by email, i don''t store them anywhere. But it looks like creating a model would really simplify things (like validation). -- Posted via http://www.ruby-forum.com/.
szymek
2006-Mar-23 18:48 UTC
[Rails] Re: How to redisplay submitted data on validation error?
I''ve created ContactForm model (using this tutorial: http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model) and added email verification to it. Now redisplaying submitted data works and the code is simpler than before :) -- Posted via http://www.ruby-forum.com/.