Wes Gamble
2006-Mar-18 18:42 UTC
[Rails] Using <%= text_field %> within partials is problematic
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] %> <%= 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 -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Mar-18 19:39 UTC
[Rails] Re: Using <%= text_field %> within partials is problematic
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 Gamble 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] %> <%= 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-- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Mar-18 19:45 UTC
[Rails] Re: Using <%= text_field %> within partials is problematic
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 Gamble 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 Gamble 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] %> <%= 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-- Posted via http://www.ruby-forum.com/.
Craig White
2006-Mar-18 19:55 UTC
[Rails] Using <%= text_field %> within partials is problematic
On Sat, 2006-03-18 at 19:42 +0100, Wes Gamble 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] %> <%= 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? >---- 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 Gamble
2006-Mar-18 19:58 UTC
[Rails] Re: Using <%= text_field %> within partials is problematic
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 Gamble 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 Gamble 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 Gamble 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] %> <%= 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-- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Mar-18 20:16 UTC
[Rails] Re: Using <%= text_field %> within partials is problematic
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 Gamble 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 Gamble 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 Gamble 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 Gamble 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] %> <%= 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-- Posted via http://www.ruby-forum.com/.
Bill Walton
2006-Mar-18 20:36 UTC
[Rails] Re: Using <%= text_field %> within partials is problematic
Wes Gamble 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.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> Thanks, > Wes > > Wes Gamble 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 Gamble 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 Gamble 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] %> <%= target_lists[:name] %><BR/> > >>> > >>> I see the id and name attributes of each member of @target_lists whenI> >>> render. > >>> > >>> However, if I do this: > >>> > >>> <%= text_field :target_lists, :name %> > >>> > >>> I get "undefined method `name'' for #<Array:0x38380f0>" on the linewith> >>> the text_field call. > >>> > >>> If I do this: > >>> > >>> <%= text_field target_lists, :name %> > >>> > >>> I get `@#<TargetList:0x3a1ae50>'' is not allowed as an instancevariable> >>> 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Chris Hall
2006-Mar-18 21:03 UTC
[Rails] Re: Using <%= text_field %> within partials is problematic
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. On 3/18/06, Wes Gamble <weyus@att.net> wrote:> > 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 Gamble 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 Gamble 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 Gamble 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 Gamble 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] %> <%= 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060318/beace621/attachment-0001.html
carmen
2006-Mar-18 22:19 UTC
[Rails] Re: Re: Using <%= text_field %> within partials is problemat
> 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.. -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Mar-19 05:32 UTC
[Rails] Re: Re: Using <%= text_field %> within partials is problemat
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..-- Posted via http://www.ruby-forum.com/.