default value if textfield is empty

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!

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.

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,

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.

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. :slight_smile: )

Best,

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@address.com" onclick =>
"clickclear(this, 'fake@address.com')" onblur => "clickrecall
(this,'fake@address.com'')" %>

Just be sure that the 3 values here are all identical.

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@address.com" onclick =>
"clickclear(this, 'fake@address.com')" onblur => "clickrecall
(this,'fake@address.com'')" %>

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,