Text_field not populating, but object exists!


#1

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?


#2

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


#3

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


#4

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

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


#5

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

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 :wink: But thats the current state of
affairs.

-Ezra


#6

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?