Forum: Ruby on Rails Using <%= text_field %> within partials is problematic

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Wes G. (Guest)
on 2006-03-18 20:42
All,

Thanks for any help in advance.

I have what I think is a very straightforward partial template and I
can't figure out why a text_field call within it doesn't work.

Here is my calling template:  lists.rhtml

<div id="target_lists">
  <%= render(:partial => "target_lists", :collection => @target_lists)
%>
</div>

In the partial template: _target_lists.rhtml, if I have the following
code,

  <%= target_lists[:id] %>&nbsp;<%= target_lists[:name] %><BR/>

I see the id and name attributes of each member of @target_lists when I
render.

However, if I do this:

  <%= text_field  :target_lists, :name %>

I get "undefined method `name' for #<Array:0x38380f0>" on the line with
the text_field call.

If I do this:

  <%= text_field  target_lists, :name %>

I get `@#<TargetList:0x3a1ae50>' is not allowed as an instance variable
name.

How can I successfully use the <%= text_field %> call within a partial
on each element of a collection that is being passed in?

Thanks,
Wes
Wes G. (Guest)
on 2006-03-18 21:39
I see that if I add

<% @target_lists = target_lists %>

to the top of this file, then all of my problems appear to go away.

Is the partial compiled and executed completely out of the context of
the including RHTML page?  That would make sense, then, because
@target_fields (which the text_field call requires to be present in
order to work) would not be defined in that case.

Thoughts?

WG

Wes G. wrote:
> All,
>
> Thanks for any help in advance.
>
> I have what I think is a very straightforward partial template and I
> can't figure out why a text_field call within it doesn't work.
>
> Here is my calling template:  lists.rhtml
>
> <div id="target_lists">
>   <%= render(:partial => "target_lists", :collection => @target_lists)
> %>
> </div>
>
> In the partial template: _target_lists.rhtml, if I have the following
> code,
>
>   <%= target_lists[:id] %>&nbsp;<%= target_lists[:name] %><BR/>
>
> I see the id and name attributes of each member of @target_lists when I
> render.
>
> However, if I do this:
>
>   <%= text_field  :target_lists, :name %>
>
> I get "undefined method `name' for #<Array:0x38380f0>" on the line with
> the text_field call.
>
> If I do this:
>
>   <%= text_field  target_lists, :name %>
>
> I get `@#<TargetList:0x3a1ae50>' is not allowed as an instance variable
> name.
>
> How can I successfully use the <%= text_field %> call within a partial
> on each element of a collection that is being passed in?
>
> Thanks,
> Wes
Wes G. (Guest)
on 2006-03-18 21:45
OK this really doesn't make any sense.

I just proved that the partial should have access to any and all
instance variables that are available in the including page AND I
further proved that the including page has access to

@target_lists

already.

So, the question is: why do I have to re-define @target_lists (per the
previous post) inside of my partial when it should already be
available???

Wes

Wes G. wrote:
> I see that if I add
>
> <% @target_lists = target_lists %>
>
> to the top of this file, then all of my problems appear to go away.
>
> Is the partial compiled and executed completely out of the context of
> the including RHTML page?  That would make sense, then, because
> @target_fields (which the text_field call requires to be present in
> order to work) would not be defined in that case.
>
> Thoughts?
>
> WG
>
> Wes G. wrote:
>> All,
>>
>> Thanks for any help in advance.
>>
>> I have what I think is a very straightforward partial template and I
>> can't figure out why a text_field call within it doesn't work.
>>
>> Here is my calling template:  lists.rhtml
>>
>> <div id="target_lists">
>>   <%= render(:partial => "target_lists", :collection => @target_lists)
>> %>
>> </div>
>>
>> In the partial template: _target_lists.rhtml, if I have the following
>> code,
>>
>>   <%= target_lists[:id] %>&nbsp;<%= target_lists[:name] %><BR/>
>>
>> I see the id and name attributes of each member of @target_lists when I
>> render.
>>
>> However, if I do this:
>>
>>   <%= text_field  :target_lists, :name %>
>>
>> I get "undefined method `name' for #<Array:0x38380f0>" on the line with
>> the text_field call.
>>
>> If I do this:
>>
>>   <%= text_field  target_lists, :name %>
>>
>> I get `@#<TargetList:0x3a1ae50>' is not allowed as an instance variable
>> name.
>>
>> How can I successfully use the <%= text_field %> call within a partial
>> on each element of a collection that is being passed in?
>>
>> Thanks,
>> Wes
Craig W. (Guest)
on 2006-03-18 21:56
(Received via mailing list)
On Sat, 2006-03-18 at 19:42 +0100, Wes G. wrote:
>   <%= render(:partial => "target_lists", :collection => @target_lists)
>
>
> I get `@#<TargetList:0x3a1ae50>' is not allowed as an instance variable
> name.
>
> How can I successfully use the <%= text_field %> call within a partial
> on each element of a collection that is being passed in?
>
----
I can't figure out if you are seeking input fields on the layout or
trying to iterate over existing values.

If you are just trying to display the values by iterating over the array
of values...try

<%= target_lists[:name] %>

Craig
Wes G. (Guest)
on 2006-03-18 21:58
Do I have to redefine @target_lists because in the including template,
it is an array, but inside of my partial it is only an element of that
array?

And so, just trying to make a call using text_field to render an
attribute of it fails because the array doesn't have any attributes,
only each member has attributes?

I think that this is right.

If anyone can verify that would be wonderful.

Thanks,
Wes

Wes G. wrote:
> OK this really doesn't make any sense.
>
> I just proved that the partial should have access to any and all
> instance variables that are available in the including page AND I
> further proved that the including page has access to
>
> @target_lists
>
> already.
>
> So, the question is: why do I have to re-define @target_lists (per the
> previous post) inside of my partial when it should already be
> available???
>
> Wes
>
> Wes G. wrote:
>> I see that if I add
>>
>> <% @target_lists = target_lists %>
>>
>> to the top of this file, then all of my problems appear to go away.
>>
>> Is the partial compiled and executed completely out of the context of
>> the including RHTML page?  That would make sense, then, because
>> @target_fields (which the text_field call requires to be present in
>> order to work) would not be defined in that case.
>>
>> Thoughts?
>>
>> WG
>>
>> Wes G. wrote:
>>> All,
>>>
>>> Thanks for any help in advance.
>>>
>>> I have what I think is a very straightforward partial template and I
>>> can't figure out why a text_field call within it doesn't work.
>>>
>>> Here is my calling template:  lists.rhtml
>>>
>>> <div id="target_lists">
>>>   <%= render(:partial => "target_lists", :collection => @target_lists)
>>> %>
>>> </div>
>>>
>>> In the partial template: _target_lists.rhtml, if I have the following
>>> code,
>>>
>>>   <%= target_lists[:id] %>&nbsp;<%= target_lists[:name] %><BR/>
>>>
>>> I see the id and name attributes of each member of @target_lists when I
>>> render.
>>>
>>> However, if I do this:
>>>
>>>   <%= text_field  :target_lists, :name %>
>>>
>>> I get "undefined method `name' for #<Array:0x38380f0>" on the line with
>>> the text_field call.
>>>
>>> If I do this:
>>>
>>>   <%= text_field  target_lists, :name %>
>>>
>>> I get `@#<TargetList:0x3a1ae50>' is not allowed as an instance variable
>>> name.
>>>
>>> How can I successfully use the <%= text_field %> call within a partial
>>> on each element of a collection that is being passed in?
>>>
>>> Thanks,
>>> Wes
Wes G. (Guest)
on 2006-03-18 22:16
OK here's the final answer which I'd like to write up in the Wiki
somewhere.

Given

<%= text_field :xyz, :abc %>

which should render a text box containing attribute 'abc' of object
'xyz'.

Because text_field is generating code under the assumption that there is
an instance variable named @xyz, you cannot count on the text_field call
to be able to find the _local_ variable xyz.

Therefore, you are required to assign xyz to an instance variable like
so:

@edf = xyz

inside your partial.

Calls to text_field that then reference :edf will work just fine.

Wes

Wes G. wrote:
> Do I have to redefine @target_lists because in the including template,
> it is an array, but inside of my partial it is only an element of that
> array?
>
> And so, just trying to make a call using text_field to render an
> attribute of it fails because the array doesn't have any attributes,
> only each member has attributes?
>
> I think that this is right.
>
> If anyone can verify that would be wonderful.
>
> Thanks,
> Wes
>
> Wes G. wrote:
>> OK this really doesn't make any sense.
>>
>> I just proved that the partial should have access to any and all
>> instance variables that are available in the including page AND I
>> further proved that the including page has access to
>>
>> @target_lists
>>
>> already.
>>
>> So, the question is: why do I have to re-define @target_lists (per the
>> previous post) inside of my partial when it should already be
>> available???
>>
>> Wes
>>
>> Wes G. wrote:
>>> I see that if I add
>>>
>>> <% @target_lists = target_lists %>
>>>
>>> to the top of this file, then all of my problems appear to go away.
>>>
>>> Is the partial compiled and executed completely out of the context of
>>> the including RHTML page?  That would make sense, then, because
>>> @target_fields (which the text_field call requires to be present in
>>> order to work) would not be defined in that case.
>>>
>>> Thoughts?
>>>
>>> WG
>>>
>>> Wes G. wrote:
>>>> All,
>>>>
>>>> Thanks for any help in advance.
>>>>
>>>> I have what I think is a very straightforward partial template and I
>>>> can't figure out why a text_field call within it doesn't work.
>>>>
>>>> Here is my calling template:  lists.rhtml
>>>>
>>>> <div id="target_lists">
>>>>   <%= render(:partial => "target_lists", :collection => @target_lists)
>>>> %>
>>>> </div>
>>>>
>>>> In the partial template: _target_lists.rhtml, if I have the following
>>>> code,
>>>>
>>>>   <%= target_lists[:id] %>&nbsp;<%= target_lists[:name] %><BR/>
>>>>
>>>> I see the id and name attributes of each member of @target_lists when I
>>>> render.
>>>>
>>>> However, if I do this:
>>>>
>>>>   <%= text_field  :target_lists, :name %>
>>>>
>>>> I get "undefined method `name' for #<Array:0x38380f0>" on the line with
>>>> the text_field call.
>>>>
>>>> If I do this:
>>>>
>>>>   <%= text_field  target_lists, :name %>
>>>>
>>>> I get `@#<TargetList:0x3a1ae50>' is not allowed as an instance variable
>>>> name.
>>>>
>>>> How can I successfully use the <%= text_field %> call within a partial
>>>> on each element of a collection that is being passed in?
>>>>
>>>> Thanks,
>>>> Wes
Bill W. (Guest)
on 2006-03-18 22:36
(Received via mailing list)
Wes G. wrote:

> If anyone can verify that would be wonderful.
Yes, but you're not redefining @target_lists.  You're passing in a
collection of records and asking the partial to render each one.  So
(right
out of AWDwRoR) you might try...

<%= start_form_tag %>
   <% for @target_list in @target_lists %>
      <%= text_field("target_list[]", "name") %>
   <% end %>
   <%= submit_tag %>
<%= end_form_tag %>

HTH,
Bill


> > @target_lists
> >> I see that if I add
> >> Thoughts?
> >>>
> >>>   <%= target_lists[:id] %>&nbsp;<%= target_lists[:name] %><BR/>
> >>>
> >>> I see the id and name attributes of each member of @target_lists when
I
> >>> render.
> >>>
> >>> However, if I do this:
> >>>
> >>>   <%= text_field  :target_lists, :name %>
> >>>
> >>> I get "undefined method `name' for #<Array:0x38380f0>" on the line
with
> >>> the text_field call.
> >>>
> >>> If I do this:
> >>>
> >>>   <%= text_field  target_lists, :name %>
> >>>
> >>> I get `@#<TargetList:0x3a1ae50>' is not allowed as an instance
variable
Chris H. (Guest)
on 2006-03-18 23:03
(Received via mailing list)
from the api docs:

<%= render :partial => "ad", :collection => @advertisements %>

 This will render "advertiser/_ad.rhtml" and pass the local variable ad
to
the template for display. An iteration counter will automatically be
made
available to the template with a name of the form partial_name_counter.
In
the case of the example above, the template would be fed ad_counter.
therefore, per your example in the first post,

<div id="target_lists">
 <%= render(:partial => "target_lists", :collection => @target_lists)
%>
</div>

render will pass in a local var called 'target_lists' to the template.
this
has nothing to do with @target_lists and everything to do with the
partial
NAME.  if you partial was named "_foobar.rhtml", then var passed to the
template would be 'foobar'.  it just so happens that in your particular
situation, the partial name and the collection have the same name, hence
the
confusion.
Carmen -. (Guest)
on 2006-03-19 00:19
> render will pass in a local var called 'target_lists' to the template.

assuming this, and assuming the form helpers only look for instance
variables and not locals, then his extra assignment @some_object =
local_partial_var is unavoidable? that sounds awfully un-railsy.

anyways thanks for the clarifications, it helped me figure out ways
around an 'instance variables created inside markably templates dont
propagate to further subtemplates' issue by just using the build in
tricks to pass in data..
Wes G. (Guest)
on 2006-03-19 07:32
Carmen,

Could you expand on your statement?  Perhaps a specific example?

Thanks,
Wes

carmen  wrote:
> anyways thanks for the clarifications, it helped me figure out ways
> around an 'instance variables created inside markably templates dont
> propagate to further subtemplates' issue by just using the build in
> tricks to pass in data..
This topic is locked and can not be replied to.