# Default values and text\_field

**URL:** <https://www.ruby-forum.com/t/default-values-and-text-field/203061>\
**Category:** Rails\
**Created:** [February 11, 2011, 2:38pm UTC](https://www.ruby-forum.com/t/default-values-and-text-field/203061 "2011-02-11T14:38:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 11, 2011, 2:38pm UTC](https://www.ruby-forum.com/t/default-values-and-text-field/203061/1 "2011-02-11T14:38:57Z")

</div>

I have a model and I set defaults for some values by overriding the  
read accessor. For example

def heading  
read\_attribute(:heading).nil? ? ‘Please select from:’ :  
read\_attribute(:heading)  
end

The problem I have found is that text\_field ignores this.

```
<%= f.text_field :heading %>

```

is empty even when heading is nil

now I can easily add :value and it works

```
<%= f.text_field :heading, :value => @site.heading %>

```

but it seems like this shouldn’t be necessary. I also override read  
accessors for values that I use in select statements on the same form  
and it works fine.

def text\_color  
read\_attribute(:text\_color).nil? ? SiteColors::TEXT\_DEFAULT :  
read\_attribute(:text\_color)  
end

\<%= f.select :text\_color, SiteColors::COLORS%\>

This picks up the default fine

Is this a bug in text\_field or am I just doing something stupid?

Tony

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [February 11, 2011, 2:46pm UTC](https://www.ruby-forum.com/t/default-values-and-text-field/203061/2 "2011-02-11T14:46:29Z")

</div>

On Feb 11, 1:37pm, Tony P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a model and I set defaults for some values by overriding the  
> read accessor. For example
> 
> def heading  
> read\_attribute(:heading).nil? ? ‘Please select from:’ :  
> read\_attribute(:heading)  
> end

Have you double checked this works by calling heading directly on the  
object in question?

Fred

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [February 11, 2011, 2:52pm UTC](https://www.ruby-forum.com/t/default-values-and-text-field/203061/3 "2011-02-11T14:52:08Z")

</div>

On Feb 11, 8:45am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Have you double checked this works by calling heading directly on the  
> object in question?

yes.

This works

\<%= f.text\_field :heading, :value =\> @site.heading %\>

and if I set it in the controller it works fine too.

@site.heading = ‘testing’

Maybe text\_field is using read\_attribute instead of calling the  
method?

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [February 11, 2011, 4:58pm UTC](https://www.ruby-forum.com/t/default-values-and-text-field/203061/4 "2011-02-11T16:58:46Z")

</div>

The string is nil. I even created a new rails project to test this  
out.

(Rails 3 / 1.9.2)

rails generate scaffold test value1:string value2:integer

app/models/test.rb  
class Test \< ActiveRecord::Base  
def value1  
read\_attribute(:value1).nil? ? ‘hello’ : read\_attribute(:value1)  
end  
def value2  
read\_attribute(:value2).nil? ? 3 : read\_attribute(:value2)  
end  
end

app/views/tests/\_form.html.erb  
\<%= form\_for(@test) do |f| %\>  
value 1 is \<%= @test.value1.inspect %\> and value 2 is \<%=  
@test.value2.inspect %\>

 \<%= f.label :value1 %\>  
 \<%= f.text\_field :value1 %\> 

 \<%= f.label :value2 %\>  
 \<%= f.select :value2, (1..10) %\> 

 \<%= f.submit %\> 
\<% end %\>

At the top of the form we see  
value 1 is “hello” and value 2 is 3

the select statement has 3 selected but the text field has nothing.

Adding ‘:value =\> @test.value1’ fixes the issue but shouldn’t be  
necessary AFAIK  
\<%= f.text\_field :value1, :value =\> @test.value1 %\>

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [February 11, 2011, 5:16pm UTC](https://www.ruby-forum.com/t/default-values-and-text-field/203061/5 "2011-02-11T17:16:59Z")

</div>

Sorry, I can’t help you out as I’m not on my dev box. One thing off the  
top  
of my mind (so you wont have to worry about that) is to use a  
placeholder  
attribute instead of doing that check. Good luck!

> **[HTML5's placeholder Attribute](https://davidwalsh.name/html5-placeholder)**
>
> The placeholder attribute shows text in a field until the field is focused upon, then hides the text.  You've seen this technique  a billion times with JavaScript!

On Fri, Feb 11, 2011 at 11:57 PM, Tony P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> read\_attribute(:value1).nil? ? ‘hello’ : read\_attribute(:value1)
> 
> \<% end %\>
> 
> > > > > end  
> > > > > and if I set it in the controller it works fine too.  
> > > > > \<%= f.text\_field :heading %\>  
> > > > > form
> > 
> > > To unsubscribe from this group, send email to  
> > > “Ruby on Rails: Talk” group.  
> > > To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> > > To unsubscribe from this group, send email to  
> > > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> > > For more options, visit this group at  
> > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [February 11, 2011, 4:01pm UTC](https://www.ruby-forum.com/t/default-values-and-text-field/203061/6 "2011-02-11T16:01:48Z")

</div>

are you sure that heading is nil? could you double check if it’s not an  
empty string?

On Fri, Feb 11, 2011 at 9:50 PM, Tony P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > ```
> > > read_attribute(:heading).nil? ? 'Please select from:' :
> > > 
> > > ```
> 
> > > read\_attribute(:text\_color)

–
