<jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org>
2005-Feb-16 20:08 UTC
Form Validation and Resubmit
Hi, Ruby newbie here. In tying to develop my own CRUD Application, I want a form that will save data, checking it with the built in Validation functions. If it meets the validation rules save the data, if not redisplay the same form with the original data. It seems to work okay, except for the ability to redisplay the original data if I need to resubmit the form due to data validation issues. I''ve searched the archives and wiki, and there doesn''t seem to be a comparable example. I''m not using the scaffolding because I want to roll my own. This is a simplifed version of what I"m trying to do (Only the showing the Create Part) ############## Database: CREATE TABLE `emails` ( `id` int(6) unsigned NOT NULL auto_increment, `name` varchar(40) NOT NULL default '''', `emailaddress` varchar(40) NOT NULL default '''', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ############## Model: class Email < ActiveRecord::Base validates_presence_of :name validates_format_of :emailaddress, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create end ############## Controller: class EmailController < ApplicationController def new end def create @item = Email.new @item.attributes = @params["email"] if @item.save render_text "Email Saved" else new render_action "new" end end end ############## View: <html> <head> <title> Add your Email </title> </head> <body> <form action="/email/create" method="POST"> Name: <%= text_field "email", "name", "size" => 25 %><br> Email Address : <%= text_field "email", "emailaddress", "size" => 45 %><br> <input type="submit" value="Add Email"> </form> <% unless @item == nil or @item.errors.empty? %> The Item Coudn''t be saved due to these errors: <BR> <% @item.errors.each_full do |message| %> <BR> <%= message %> <% end%> <% end %> </body> </html> ############## END So what''s the best way to get the data to redisplay? Also a couple general questions 1) Which is the best way to redisplay the form on validation fail? I read that a redirect should work, but it didn''t. So I called the new and then the render_action 2) The error messsages are ugly because the display the actual field name i.e. Emailaddress is invalid, i know I can fix the "is invalid part" but how can I make a pretty field name, for example "The Email address you entered is invalid" 3) How to display some text or "!" gif next to the field that was invalid to make it easy to locate the field with the problem. 4) I think I''m on a recent build, I just downloaaded the install a couple days ago, but how can I tell the version? Thanks, Joe
I''d reccomend, before doing anything else, that you run script/generate scaffold and read the generated source. (#1) Look at ActiveRecord#error_messages_for and ActiveRecord#error_message_on, too. If you customize your validation messages and then use error_message_on, you can report field specific errors. You can customize the messages by passing :message=>''error_message'' to your calls to validation functions in the model. (#2, #3) If you''ve installed rails through gems then do ''gem list rails'' and it will print out all installed versions. I believe the first one in the displayed list is the most recent and is used by default. (#4) Good luck, Brian On Wed, 16 Feb 2005 12:08:05 -0800, jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote:> Hi, > Ruby newbie here. > > In tying to develop my own CRUD Application, I want a form > that will save data, checking it with the built in > Validation functions. > If it meets the validation rules save the data, if not > redisplay the same form with the original data. > It seems to work okay, except for the ability to redisplay > the original data if I need to resubmit the form due to > data validation issues. I''ve searched the archives and > wiki, and there doesn''t seem to be a comparable example. > > I''m not using the scaffolding because I want to roll my > own. > > This is a simplifed version of what I"m trying to do (Only > the showing the Create Part) > > ############## Database: > CREATE TABLE `emails` ( > `id` int(6) unsigned NOT NULL auto_increment, > `name` varchar(40) NOT NULL default '''', > `emailaddress` varchar(40) NOT NULL default '''', > PRIMARY KEY (`id`) > ) ENGINE=InnoDB DEFAULT CHARSET=latin1; > > ############## Model: > class Email < ActiveRecord::Base > validates_presence_of :name > validates_format_of :emailaddress, :with => > /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create > end > > ############## Controller: > class EmailController < ApplicationController > > def new > end > > def create > @item = Email.new > @item.attributes = @params["email"] > > if @item.save > render_text "Email Saved" > else > new > render_action "new" > > end > > end > end > > ############## View: > > <html> > <head> > <title> > Add your Email > </title> > </head> > <body> > > <form action="/email/create" method="POST"> > Name: <%= text_field "email", "name", "size" => 25 %><br> > Email Address : <%= text_field "email", "emailaddress", > "size" => 45 %><br> > <input type="submit" value="Add Email"> > </form> > > <% unless @item == nil or @item.errors.empty? %> > The Item Coudn''t be saved due to these errors: > <BR> > <% @item.errors.each_full do |message| %> > <BR> > <%= message %> > <% end%> > <% end %> > </body> > </html> > > ############## END > > So what''s the best way to get the data to redisplay? > > Also a couple general questions > > 1) Which is the best way to redisplay the form on > validation fail? I read that a redirect should work, but > it didn''t. So I called the new and then the render_action > > 2) The error messsages are ugly because the display the > actual field name i.e. Emailaddress is invalid, i know I > can fix the "is invalid part" but how can I make a pretty > field name, for example "The Email address you entered is > invalid" > > 3) How to display some text or "!" gif next to the field > that was invalid to make it easy to locate the field with > the problem. > > 4) I think I''m on a recent build, I just downloaaded the > install a couple days ago, but how can I tell the version? > > Thanks, > > Joe > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
I did that, and while it was informative, it didn''t exactly answer my question. The scaffold calls a form helper to generate the form in the view [New], while I want to handcode the view. It seems that through the "magic" of <%= form ''email'', :action => ''create''%> It generates the following html to prepopulate the field: <input id="email_emailaddress" name="email[emailaddress]" size="30" type="text" value="sfdgsdfg" /> Let''s say I don''t want to use the form helper, how would I hand code the html (using the form helper text_field) I tried this thinking that by setting the value of "Value" it would display the previous value, but it seems to bomb out on my syntax. What would be the proper way to refer to item.email in this context? Email Address : <%= text_field "email", "emailaddress", "size" => 45, "value" => @item.email %><br> Thanks, Joe -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Brian L. Sent: Wednesday, February 16, 2005 8:16 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Form Validation and Resubmit I''d reccomend, before doing anything else, that you run script/generate scaffold and read the generated source. (#1) Look at ActiveRecord#error_messages_for and ActiveRecord#error_message_on, too. If you customize your validation messages and then use error_message_on, you can report field specific errors. You can customize the messages by passing :message=>''error_message'' to your calls to validation functions in the model. (#2, #3) If you''ve installed rails through gems then do ''gem list rails'' and it will print out all installed versions. I believe the first one in the displayed list is the most recent and is used by default. (#4) Good luck, Brian
Hi Joe, There''s no need to use two different actions for what you''re doing. Here''s an example of how to use one action for both displaying the form and to do the db insertion if all the preconditions are met. Note that the same view file is also used for both create and edit actions. def add_sole @target = "add" case @request.method when :get @sole = Sole.new render_action "edit_sole" when :post @sole = Sole.new(@params["sole"]) if @sole.valid? Sole.transaction do raise "rollback" unless @sole.save redirect_to :action => "details" end else # render_action "edit_sole" render_text "edit_sole" end end end ## edit-sole.rhtml ## <%= form_tag :action => "#{@target}_sole" %> <%= hidden_field "sole", "id" %> <dl> <dt><label for="sole_code">Koodi</label></dt> <dd><%= text_field "sole", "code", "size" => 10 %></dd> <dt><label for="sole_name_en">Englanninkielinen nimi</label></dt> <dd><%= text_field "sole", "name_en", "size" => 10 %></dd> <dt><label for="sole_name_fi">Suomenkielinen nimi</label></dt> <dd><%= text_field "sole", "name_fi", "size" => 30 %></dd> <dt><input type="submit" id="form_submit" value="Tallenna »" /></dt> </dl> <%= end_form_tag %> ### A few things to notice: * @target is used to route the form to the right action (add or edit) * We check if the request is a new request (to show the form) or a post request (to submit the form) from the @request object and proceed accordingly. * @sole is either a new Sole object or the object created from the form input. Then the form will automatically show the inserted values in the form fields if the validation fails. In your case you created an object @item, but the in the form you used @email. Thus Rails couldn''t pre-fill your form. On 16.2.2005, at 22:08, <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote:> > 1) Which is the best way to redisplay the form on validation fail? I > read that a redirect should work, but it didn''t. So I called the new > and then the render_actionI hope I already answered this. Note that when the submission failed, rails will automatically add a special css class to your form inputs, so you can make them easy to find.> > 2) The error messsages are ugly because the display the actual field > name i.e. Emailaddress is invalid, i know I can fix the "is invalid > part" but how can I make a pretty field name, for example "The Email > address you entered is invalid"I don''t think this is possible with the generic validation methods at the moment. If you need this, you might want to try building your own custom validations (as described here: http://rails.rubyonrails.com/classes/ActiveRecord/Validations.html)> > 3) How to display some text or "!" gif next to the field that was > invalid to make it easy to locate the field with the problem.Use css to display a background image in elements with "fieldWithErrors" class. //jarkko -- 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 Wed, 16 Feb 2005 12:08:05 -0800, jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote:> def create > @item = Email.new > @item.attributes = @params["email"][...]> render_action "new"[...]> Name: <%= text_field "email", "name", "size" => 25 %><br>The two names (@item and "email") have to correspond. Remember that you are now rendering the view "new" from the create action, so you have to either rename @item to @email or the other way around, so that the first argument to text_field matches the name of your object (instance variable). Whatever instance variables you set up in the "new" action are not available to the "create" action. I''m a newbie as well, so take this with a grain of salt. That said, I think it''s correct :) -- Regards, Stian Grytøyr
Thanks everyone for your help. I got it to work with your tips, for future reference here is the full code in case someone wants a simple example of form validation in the future. I had a disconnect between the variables in the HTML and my Controller (Using @Item in the Controller and "Email" in the text_field() in the HTML) Joe ############## Database: CREATE TABLE `emails` ( `id` int(6) unsigned NOT NULL auto_increment, `name` varchar(40) NOT NULL default '''', `emailaddress` varchar(40) NOT NULL default '''', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ############## Model: class Email < ActiveRecord::Base validates_presence_of :name validates_format_of :emailaddress, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create end ############## Controller: class EmailController < ApplicationController def new @email = Email.new end def create @email = Email.new @email.attributes = @params["email"] if @email.save render_text "Email Saved" else render_action "new" end end end ############## View: <html> <head> <title> Add your Email </title> </head> <body> <form action="/email/create" method="POST"> Name: <%= text_field "email", "name", "size" => 25 %><br> Email Address : <%= text_field "email", "emailaddress", "size" => 45 %><br> <input type="submit" value="Add Email"> </form> <% unless @email == nil or @email.errors.empty? %> The Item Coudn''t be saved due to these errors: <BR> <% @email.errors.each_full do |message| %> <BR> <%= message %> <% end%> <% end %> </body> </html>
Hey Joe, Thanks for the code. Just a question, in yr ''def new'' method, what is the purpose of creating the @email object? Is it for the form input to be populated upon errors? Also, I''m impressed that the form fields are html-encoded by Rails to prevent XSS :) rgds On Thu, 17 Feb 2005 17:25:24 -0800, Joseph Lyons <JML-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote:> Thanks everyone for your help. > I got it to work with your tips, for future reference here is the full code > in case someone wants a simple example of form validation in the future. > I had a disconnect between the variables in the HTML and my Controller > (Using @Item in the Controller and "Email" in the text_field() in the HTML) > > Joe > > ############## Database: > CREATE TABLE `emails` ( > `id` int(6) unsigned NOT NULL auto_increment, > `name` varchar(40) NOT NULL default '''', > `emailaddress` varchar(40) NOT NULL default '''', > PRIMARY KEY (`id`) > ) ENGINE=InnoDB DEFAULT CHARSET=latin1; > > ############## Model: > class Email < ActiveRecord::Base > validates_presence_of :name > validates_format_of :emailaddress, :with => > /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create > end > > ############## Controller: > class EmailController < ApplicationController > > def new > @email = Email.new > end > > def create > @email = Email.new > @email.attributes = @params["email"] > > if @email.save > render_text "Email Saved" > else > render_action "new" > > end > > end > end > > ############## View: > <html> > <head> > <title> > Add your Email > </title> > </head> > <body> > > <form action="/email/create" method="POST"> > Name: <%= text_field "email", "name", "size" => 25 %><br> > Email Address : <%= text_field "email", "emailaddress", "size" => 45 %><br> > <input type="submit" value="Add Email"> > </form> > > <% unless @email == nil or @email.errors.empty? %> > The Item Coudn''t be saved due to these errors: > <BR> > <% @email.errors.each_full do |message| %> > <BR> > <%= message %> > <% end%> > <% end %> > </body> > </html> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
<jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org>
2005-Feb-18 03:29 UTC
Re: Form Validation and Resubmit
BoonHoo, By creating the @email object prior displaying the form allows the attributes of the object to default to any predefinded values (For Example I have some defaults in my database that automatically come up in the form) I don''t think that this any anything to do with the errors, because the errors get populuated in the Create method, and when the ''render_action register'' is called (only if there is an error) my understanding is that it just rerenders the document, and therefore the values of @email in the create method is used rather than creating the ojbect again (in the register method). I "Think" that when you call render_action it doesn''t actually run the code in the method for that page... it just displays the page with the current state of the objects. But since I"ve been doing this for a total of 3 days, I could be completely wrong. Joe On Fri, 18 Feb 2005 11:23:40 +0800 boonhoo <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:>Hey Joe, > >Thanks for the code. Just a question, in yr ''def new'' >method, what is >the purpose of creating the @email object? Is it for the >form input to >be populated upon errors? > >Also, I''m impressed that the form fields are html-encoded >by Rails to >prevent XSS :) > >rgds
THanks Joe. Have you tried caching the page? I added ''caches_page :new, :create'' to the EmailController, but it does not seem to work. Every access to the ''new'' action seems to be dynamically generated (CPU % spike to 100) and I couldn''t find any static files generated in the ''public'' folder. Does anyone know if there''s anything else I need to set to enable caching? On Thu, 17 Feb 2005 19:29:17 -0800, jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote:> BoonHoo, > By creating the @email object prior displaying the form > allows the attributes of the object to default to any > predefinded values (For Example I have some defaults in my > database that automatically come up in the form) > > I don''t think that this any anything to do with the > errors, because the errors get populuated in the Create > method, and when the ''render_action register'' is called > (only if there is an error) my understanding is that it > just rerenders the document, and therefore the values of > @email in the create method is used rather than creating > the ojbect again (in the register method). I "Think" > that when you call render_action it doesn''t actually run > the code in the method for that page... it just displays > the page with the current state of the objects. > > But since I"ve been doing this for a total of 3 days, I > could be completely wrong. > > Joe > > On Fri, 18 Feb 2005 11:23:40 +0800 > boonhoo <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >Hey Joe, > > > >Thanks for the code. Just a question, in yr ''def new'' > >method, what is > >the purpose of creating the @email object? Is it for the > >form input to > >be populated upon errors? > > > >Also, I''m impressed that the form fields are html-encoded > >by Rails to > >prevent XSS :) > > > >rgds >
On 18.2.2005, at 07:57, boonhoo wrote:> THanks Joe. Have you tried caching the page? > > I added ''caches_page :new, :create'' to the EmailController, but it > does not seem to work. Every access to the ''new'' action seems to be > dynamically generated (CPU % spike to 100) and I couldn''t find any > static files generated in the ''public'' folder. > > Does anyone know if there''s anything else I need to set to enable > caching?You might want to cache the new action if it gets hit very often, but there''s no idea caching create, because it''s different for every entry. Even new should be so light an action that it''s probably not worth starting your code optimization from input form actions. //jarkko> > > On Thu, 17 Feb 2005 19:29:17 -0800, jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org > <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote: >> BoonHoo, >> By creating the @email object prior displaying the form >> allows the attributes of the object to default to any >> predefinded values (For Example I have some defaults in my >> database that automatically come up in the form) >> >> I don''t think that this any anything to do with the >> errors, because the errors get populuated in the Create >> method, and when the ''render_action register'' is called >> (only if there is an error) my understanding is that it >> just rerenders the document, and therefore the values of >> @email in the create method is used rather than creating >> the ojbect again (in the register method). I "Think" >> that when you call render_action it doesn''t actually run >> the code in the method for that page... it just displays >> the page with the current state of the objects. >> >> But since I"ve been doing this for a total of 3 days, I >> could be completely wrong. >> >> Joe >> >> On Fri, 18 Feb 2005 11:23:40 +0800 >> boonhoo <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> Hey Joe, >>> >>> Thanks for the code. Just a question, in yr ''def new'' >>> method, what is >>> the purpose of creating the @email object? Is it for the >>> form input to >>> be populated upon errors? >>> >>> Also, I''m impressed that the form fields are html-encoded >>> by Rails to >>> prevent XSS :) >>> >>> rgds >> > _______________________________________________ > 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 18.2.2005, at 05:29, <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote:> BoonHoo, > By creating the @email object prior displaying the form allows the > attributes of the object to default to any predefinded values (For > Example I have some defaults in my database that automatically come up > in the form)Right, and you can''t even use form helpers unless you have an object to tie them to. //jarkko -- 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 18.2.2005, at 03:25, Joseph Lyons wrote:> > ############## Controller: > class EmailController < ApplicationController > > def new > @email = Email.new > end > > def create > @email = Email.new > @email.attributes = @params["email"]Instead of the two lines above you can also say just: @email = Email.new(@params["email"]) //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Oh I meant to test out the caching feature only, not to implement it seriously:) So how do I enable caching for the ''new'' action? by adding the ''caches_page :new'' to the EmailController doesn''t seem to work...I assume Rails would create a static page somewhere in the public folder? On Fri, 18 Feb 2005 09:42:02 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > On 18.2.2005, at 07:57, boonhoo wrote: > > > THanks Joe. Have you tried caching the page? > > > > I added ''caches_page :new, :create'' to the EmailController, but it > > does not seem to work. Every access to the ''new'' action seems to be > > dynamically generated (CPU % spike to 100) and I couldn''t find any > > static files generated in the ''public'' folder. > > > > Does anyone know if there''s anything else I need to set to enable > > caching? > > You might want to cache the new action if it gets hit very often, but > there''s no idea caching create, because it''s different for every entry. > Even new should be so light an action that it''s probably not worth > starting your code optimization from input form actions. > > //jarkko > > > > > > > > On Thu, 17 Feb 2005 19:29:17 -0800, jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org > > <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote: > >> BoonHoo, > >> By creating the @email object prior displaying the form > >> allows the attributes of the object to default to any > >> predefinded values (For Example I have some defaults in my > >> database that automatically come up in the form) > >> > >> I don''t think that this any anything to do with the > >> errors, because the errors get populuated in the Create > >> method, and when the ''render_action register'' is called > >> (only if there is an error) my understanding is that it > >> just rerenders the document, and therefore the values of > >> @email in the create method is used rather than creating > >> the ojbect again (in the register method). I "Think" > >> that when you call render_action it doesn''t actually run > >> the code in the method for that page... it just displays > >> the page with the current state of the objects. > >> > >> But since I"ve been doing this for a total of 3 days, I > >> could be completely wrong. > >> > >> Joe > >> > >> On Fri, 18 Feb 2005 11:23:40 +0800 > >> boonhoo <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>> Hey Joe, > >>> > >>> Thanks for the code. Just a question, in yr ''def new'' > >>> method, what is > >>> the purpose of creating the @email object? Is it for the > >>> form input to > >>> be populated upon errors? > >>> > >>> Also, I''m impressed that the form fields are html-encoded > >>> by Rails to > >>> prevent XSS :) > >>> > >>> rgds > >> > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > >
Thanks Jarkko. I tried the caching, and even restart Rails. The log file shows the following whenever I reload the browser: [4;33mEmail Columns (0.000000) [1;37mSHOW FIELDS FROM emails Rendering email/new (200 OK) [4;35mEmail Columns (0.000000) [0;37mSHOW FIELDS FROM emails Completed in 0.190000 (5 reqs/sec) | Rendering: 0.010000 (5%) | DB: 0.000000 (0%) There is a sql query in the log file. Also, the db% may fluctuate to 50 or 100%. Does that mean there was no caching? Thanks in advance. On Fri, 18 Feb 2005 10:11:01 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > On 18.2.2005, at 10:00, boonhoo wrote: > > > Oh I meant to test out the caching feature only, not to implement it > > seriously:) > > > > So how do I enable caching for the ''new'' action? by adding the > > ''caches_page :new'' to the EmailController doesn''t seem to work...I > > assume Rails would create a static page somewhere in the public > > folder? > > The default store for cached items is MemoryStore (see > http://rails.rubyonrails.com/classes/ActionController/Caching/ > Fragments.html) so you won''t see any temp files anywhere. > > The easiest way to see if caching is kicking in is to follow the log > file while requesting the page. However, it would be easier to test the > feature with a more db intensive page. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > >
You need to have caching enabled in your environment (production does, development doesn''t, by default) Also, it''s not going to put them in public. It will store them in memory or maybe in tempspace, depending on configuration. Cache things that are very static. Anything mildly dynamic should be left uncached until performance really becomes an issue and it''s time to get creative. The only things I cache in my app are images from the database. Brian On Fri, 18 Feb 2005 13:57:02 +0800, boonhoo <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> THanks Joe. Have you tried caching the page? > > I added ''caches_page :new, :create'' to the EmailController, but it > does not seem to work. Every access to the ''new'' action seems to be > dynamically generated (CPU % spike to 100) and I couldn''t find any > static files generated in the ''public'' folder. > > Does anyone know if there''s anything else I need to set to enable caching? > > > On Thu, 17 Feb 2005 19:29:17 -0800, jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote: > > BoonHoo, > > By creating the @email object prior displaying the form > > allows the attributes of the object to default to any > > predefinded values (For Example I have some defaults in my > > database that automatically come up in the form) > > > > I don''t think that this any anything to do with the > > errors, because the errors get populuated in the Create > > method, and when the ''render_action register'' is called > > (only if there is an error) my understanding is that it > > just rerenders the document, and therefore the values of > > @email in the create method is used rather than creating > > the ojbect again (in the register method). I "Think" > > that when you call render_action it doesn''t actually run > > the code in the method for that page... it just displays > > the page with the current state of the objects. > > > > But since I"ve been doing this for a total of 3 days, I > > could be completely wrong. > > > > Joe > > > > On Fri, 18 Feb 2005 11:23:40 +0800 > > boonhoo <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >Hey Joe, > > > > > >Thanks for the code. Just a question, in yr ''def new'' > > >method, what is > > >the purpose of creating the @email object? Is it for the > > >form input to > > >be populated upon errors? > > > > > >Also, I''m impressed that the form fields are html-encoded > > >by Rails to > > >prevent XSS :) > > > > > >rgds > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
THank you for your reply. Again I stress, I am testing out the caching feature more out of curiosity, to determine whetehr rails is the correct platform for my next project. So if i want to switch to production mode, I just need to change the ''RAILS_ENV'' parameter in the environment.rb file? (Can''t find any documentation on this in the HowTos) If I want to test caching in development, how do I enabled it? Thanks again. Rgds On Fri, 18 Feb 2005 23:05:05 -0500, Brian L. <zorander-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You need to have caching enabled in your environment (production does, > development doesn''t, by default) > > Also, it''s not going to put them in public. It will store them in > memory or maybe in tempspace, depending on configuration. > > Cache things that are very static. Anything mildly dynamic should be > left uncached until performance really becomes an issue and it''s time > to get creative. The only things I cache in my app are images from the > database. > > Brian > > > On Fri, 18 Feb 2005 13:57:02 +0800, boonhoo <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > THanks Joe. Have you tried caching the page? > > > > I added ''caches_page :new, :create'' to the EmailController, but it > > does not seem to work. Every access to the ''new'' action seems to be > > dynamically generated (CPU % spike to 100) and I couldn''t find any > > static files generated in the ''public'' folder. > > > > Does anyone know if there''s anything else I need to set to enable caching? > > > > > > On Thu, 17 Feb 2005 19:29:17 -0800, jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org <jml-IW2WV5XWFqHk1uMJSBkQmQ@public.gmane.org> wrote: > > > BoonHoo, > > > By creating the @email object prior displaying the form > > > allows the attributes of the object to default to any > > > predefinded values (For Example I have some defaults in my > > > database that automatically come up in the form) > > > > > > I don''t think that this any anything to do with the > > > errors, because the errors get populuated in the Create > > > method, and when the ''render_action register'' is called > > > (only if there is an error) my understanding is that it > > > just rerenders the document, and therefore the values of > > > @email in the create method is used rather than creating > > > the ojbect again (in the register method). I "Think" > > > that when you call render_action it doesn''t actually run > > > the code in the method for that page... it just displays > > > the page with the current state of the objects. > > > > > > But since I"ve been doing this for a total of 3 days, I > > > could be completely wrong. > > > > > > Joe > > > > > > On Fri, 18 Feb 2005 11:23:40 +0800 > > > boonhoo <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >Hey Joe, > > > > > > > >Thanks for the code. Just a question, in yr ''def new'' > > > >method, what is > > > >the purpose of creating the @email object? Is it for the > > > >form input to > > > >be populated upon errors? > > > > > > > >Also, I''m impressed that the form fields are html-encoded > > > >by Rails to > > > >prevent XSS :) > > > > > > > >rgds > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > The years ahead pick up their dark bags. > They move closer. There''s a slight rise in the silence > > then nothing. > -- > (If you''re receiving this in response to mail sent to > bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, > but mail will be forwarded here indefinitely) >