Hey all, im not sure where to set a default value if a textfield is empty? For now i do it in the controller, and check if the submitted parameter is blank. But this seems not to be the best solution... Thanks for your help! -- Posted via http://www.ruby-forum.com/.
On Aug 13, 2:14 pm, Stephan Meier <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hey all, > > im not sure where to set a default value if a textfield is empty? > > For now i do it in the controller, and check if the submitted parameter > is blank. > But this seems not to be the best solution... > > Thanks for your help! > -- > Posted viahttp://www.ruby-forum.com/.What is the purpose of the default value? Do you want a newly created row in the database to have a default value if none is specified? If so, thats where the default value goes. Do you need some sort of default value when processing some sort of logic that the form is the input source for? Then the default value should go somewhere close to where the logic is happening.
I simple want to set a default email address if the user has no one entered. -- Posted via http://www.ruby-forum.com/.
Stephan Meier wrote:> I simple want to set a default email address if the user has no one > entered.How would this be of any use at all? If a user has no e-mail address, just leave the field blank rather than polluting it with bogus data. Or is there something I don''t understand here? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Aug 13, 2:29 pm, Stephan Meier <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I simple want to set a default email address if the user has no one > entered. > -- > Posted viahttp://www.ruby-forum.com/.ok! http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html does anything on this page seem like a fit ?
Stephan Meier wrote:> I simple want to set a default email address if the user has no one > entered.ok, i solved it with a simple before_save callback... and do the check inside the model. -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser wrote:> Stephan Meier wrote: >> I simple want to set a default email address if the user has no one >> entered. > > How would this be of any use at all? If a user has no e-mail address, > just leave the field blank rather than polluting it with bogus data. > Or is there something I don''t understand here? > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgThe Email Address is simple for the sender field to personalize the emails. But if the user dont want this behavior he can leave the field blank and we save our no-reply email address... :) -- Posted via http://www.ruby-forum.com/.
Stephan Meier wrote:> Marnen Laibow-Koser wrote: >> Stephan Meier wrote: >>> I simple want to set a default email address if the user has no one >>> entered. >> >> How would this be of any use at all? If a user has no e-mail address, >> just leave the field blank rather than polluting it with bogus data. >> Or is there something I don''t understand here? >> >> Best, >> -- >> Marnen Laibow-Koser >> http://www.marnen.org >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > The Email Address is simple for the sender field to personalize the > emails. > But if the user dont want this behavior he can leave the field blank and > we save our no-reply email address... :)Don''t save your no-reply e-mail address to the DB, then. Just leave the e-mail field blank in the DB, and use something like @user.email || default_email where you need it in your app. Right now, you are saving bogus data to your DB -- and what happens if your no-reply address changes? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser wrote: [...]> Don''t save your no-reply e-mail address to the DB, then. Just leave the > e-mail field blank in the DB, and use something like > > @user.email || default_email > > where you need it in your app. Right now, you are saving bogus data to > your DB -- and what happens if your no-reply address changes?It has just occurred to me that there''s one scenario in which I *would* recommend saving the default address. If you want a record of each message exactly as it went out -- including the no-reply address used *at the time*, even if it''s obsolete -- then you should save the default address to the DB. In almost any other case, this behavior is Plain Wrong. (Yes, that''s a technical computer science term. :) ) Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
It sounds like you want the default value to act sort of like a label, if so here''s a little snippet I''ve used previously that works very nicely. http://codesnippets.joyent.com/posts/show/478 Then format your input like this... <%= form.text_field :email, :value => "fake-Jbz+COfnBihBDgjK7y7TUQ@public.gmane.org" onclick => "clickclear(this, ''fake-Jbz+COfnBihBDgjK7y7TUQ@public.gmane.org'')" onblur => "clickrecall (this,''fake-Jbz+COfnBihBDgjK7y7TUQ@public.gmane.org'''')" %> Just be sure that the 3 values here are all identical. On Aug 13, 1:14 pm, Stephan Meier <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hey all, > > im not sure where to set a default value if a textfield is empty? > > For now i do it in the controller, and check if the submitted parameter > is blank. > But this seems not to be the best solution... > > Thanks for your help! > -- > Posted viahttp://www.ruby-forum.com/.
Dave S wrote:> It sounds like you want the default value to act sort of like a label, > if so here''s a little snippet I''ve used previously that works very > nicely. > > http://codesnippets.joyent.com/posts/show/478 > > Then format your input like this... > > <%= form.text_field :email, :value => "fake-Jbz+COfnBihBDgjK7y7TUQ@public.gmane.org" onclick => > "clickclear(this, ''fake-Jbz+COfnBihBDgjK7y7TUQ@public.gmane.org'')" onblur => "clickrecall > (this,''fake-Jbz+COfnBihBDgjK7y7TUQ@public.gmane.org'''')" %> > > Just be sure that the 3 values here are all identical.I haven''t looked at the snippet yet, but your last sentence is troublesome. If it''s important to have the same value in 3 places, then it should be set only once and referred to (in a variable, or with a higher level abstraction). It is *never* OK to have the same literal value in 3 places like this. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Apparently Analagous Threads
- has_many through , or habtm , using form
- Authlogic Password confirmation is too short Error. NEED HELP.
- acts_as_list / acts_as_tree / acts_as_nested_set - which one
- <b> tag in HTML 5 (was Re: Re: assert_select for <p><b>text</b>value</p>)
- Weird issue with converting floats to integer