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!

I simple want to set a default email address if the user has no one
entered.

On Aug 13, 2:14 pm, Stephan M. [email protected]
wrote:

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.

Stephan M. 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
[email protected]

On Aug 13, 2:29 pm, Stephan M. [email protected]
wrote:

I simple want to set a default email address if the user has no one
entered.

Posted viahttp://www.ruby-forum.com/.

ok!

does anything on this page seem like a fit ?

Stephan M. 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:

Stephan M. 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
[email protected]

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… :slight_smile:

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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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 => “[email protected]” onclick =>
“clickclear(this, ‘[email protected]’)” onblur => “clickrecall
(this,‘[email protected]’')” %>

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

On Aug 13, 1:14 pm, Stephan M. [email protected]

Stephan M. wrote:

Marnen Laibow-Koser wrote:

Stephan M. 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
[email protected]

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… :slight_smile:

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
[email protected]

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 => “[email protected]” onclick =>
“clickclear(this, ‘[email protected]’)” onblur => “clickrecall
(this,‘[email protected]’')” %>

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
[email protected]