# Text\_field not populating, but object exists!

**URL:** <https://www.ruby-forum.com/t/text-field-not-populating-but-object-exists/72427>\
**Category:** Rails\
**Created:** [February 23, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/text-field-not-populating-but-object-exists/72427 "2006-02-23T17:47:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [February 23, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/text-field-not-populating-but-object-exists/72427/1 "2006-02-23T17:47:48Z")

</div>

Hi I’m having a problem with a little code here, hopefully it’s easy to  
see what’s happening:

\<% @lectures.each do |lecture| %\>

```
<%= lecture.location%><br />
<%= text_field 'lecture', 'location' %>

```

\<% end %\>

The output I get is the text of the ‘location’ rendering correctly, and  
then an empty text input box! If the text can render then I must have  
an object, so why doesn’t the text\_field create the text input box?

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [February 23, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/text-field-not-populating-but-object-exists/72427/2 "2006-02-23T18:15:51Z")

</div>

On Thu, Feb 23, 2006 at 05:47:48PM +0100, Adam wrote:

> Hi I’m having a problem with a little code here, hopefully it’s easy to  
> see what’s happening:
> 
> \<% @lectures.each do |lecture| %\>  
> \<%= lecture.location%\>  
>   
> \<%= text\_field ‘lecture’, ‘location’ %\>  
> \<% end %\>

Take a look at the rendered HTML and I believe you’ll find  
the following:

```
value="#{@lecture.location}"

```

In other words it is looking for an instance variable named  
lecture in the current object. In your case lecture is  
a variable defined in the scope of the iterator.

One way around it (and I’m pretty sure this isn’t the best  
practices solution) would be to define a partial and do  
something like the following:

\<% render :partial =\> :lecture, :collection =\> @lectures %\>

-steve

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [February 23, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/text-field-not-populating-but-object-exists/72427/3 "2006-02-23T20:44:41Z")

</div>

Okay I know it is bad form to reply to myself but just to clarify  
what I was saying… The first argument to text\_field is expected  
to be an instance variable of the current object. Here’s an  
example:

% script/console  
Loading development environment.

> > include ActionView::Helpers::FormHelper  
> > =\> Object
> 
> > class Lecture  
> > attr :loc  
> > def initialize(l)  
> > @loc = l  
> > end  
> > end  
> > =\> nil
> 
> > @foo = Lecture.new :instance  
> > =\> #\<Lecture:0x8e12e44 @loc=:instance\>
> 
> > foo = Lecture.new :local  
> > =\> #\<Lecture:0x8e111ac @loc=:local\>
> 
> > bar = Lecture.new :another\_local  
> > =\> #\<Lecture:0x8e0f348 @loc=:another\_local\>
> 
> > text\_field :foo, :loc  
> > =\> “\<input id=“foo\_loc” name=“foo[loc]” size=“30” type=“text”  
> > value=“instance” /\>”
> 
> > text\_field :bar, :loc  
> > =\> “\<input id=“bar\_loc” name=“bar[loc]” size=“30” type=“text”  
> > /\>”

Notice in the first call to text\_field @foo was used and not the  
local variable foo. In the second call @bar is nil so text\_field  
simply leaves off the ‘value=…’ part.

If you look at the HTML rendered by your code:

> > \<% @lectures.each do |lecture| %\>  
> > \<%= lecture.location%\>  
> >   
> > \<%= text\_field ‘lecture’, ‘location’ %\>  
> > \<% end %\>

I believe you will find that the value attribute of the input field  
is missing because @lecture is nil. Hope that clears things up.

-steve

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [February 23, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/text-field-not-populating-but-object-exists/72427/4 "2006-02-23T22:13:16Z")

</div>

On Feb 23, 2006, at 8:47 AM, Adam wrote:

> \<% end %\>
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Adam-

```
Try this instead:

```

\<% @lectures.each do |@lecture| %\>

```
 <%= @lecture.location%><br />
 <%= text_field 'lecture', 'location' %>

```

\<% end %\>

That will make it work. The text field is expecting an instance  
variable and not a local variable. Thats why the @ sign will make  
this work for you.

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [February 24, 2006, 7:16am UTC](https://www.ruby-forum.com/t/text-field-not-populating-but-object-exists/72427/5 "2006-02-24T07:16:05Z")

</div>

On Feb 23, 2006, at 8:17 PM, Ben M. wrote:

> Hey EZ, wouldn’t the arg to text\_field need to be ‘@lecture’ then?  
> And why is it that text\_field has to have an instance var anyway?
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Ben-

```
No its a little inconsistency in the rails api there. The string

```

that you pass for the object into the form field helper needs to be  
‘foo’ and the instance var it references needs to be @foo. It is just  
the way it is. I’m not defending it 😉 But thats the current state of  
affairs.

-Ezra

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [February 24, 2006, 5:17am UTC](https://www.ruby-forum.com/t/text-field-not-populating-but-object-exists/72427/6 "2006-02-24T05:17:20Z")

</div>

Ezra Z. wrote:

> That will make it work. The text field is expecting an instance  
> variable and not a local variable. Thats why the @ sign will make this  
> work for you.

Hey EZ, wouldn’t the arg to text\_field need to be ‘@lecture’ then? And  
why is it that  
text\_field has to have an instance var anyway?
