I''m tranforming my catalyst app over to a rails app and I''m stumped again with the template. I guess it''s as much of a ruby question as a rails question but here goes. I know i can say <% for product in products %> <%= product.size %> <%end%> but how can I reference a specific element of products outside of the loop? Also how can I determine which element I''m on in the loop? I keep trying to do it in a perlish way which of course breaks. I''ve searched but I can''t find an answere. -- Posted via http://www.ruby-forum.com/.
The best way to do this is: <% products.each_with_index { |product, index| %> <%= product.size %> <% } %> You should forget *for*, it is not needed in Ruby Land. Enumerations such as *each* are more efficient when you get to more interesting cases - check out the documentation for Enumerable (http://ruby-doc.org/core/classes/Enumerable.html) and Array (http://ruby-doc.org/core/classes/Array.html) Forget the For loop - it''s so 1998! -----Original Message----- From: charlie bowman [mailto:cbowmanschool-/E1597aS9LQAvxtiuMwx3w@public.gmane.org] Sent: Monday, December 12, 2005 8:20 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] templates and arrays I''m tranforming my catalyst app over to a rails app and I''m stumped again with the template. I guess it''s as much of a ruby question as a rails question but here goes. I know i can say <% for product in products %> <%= product.size %> <%end%> but how can I reference a specific element of products outside of the loop? Also how can I determine which element I''m on in the loop? I keep trying to do it in a perlish way which of course breaks. I''ve searched but I can''t find an answere. -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
<newbie warning> I am one, so take all I say with a pinch of salt </ warning> if you have an array called @products then you can access the third element the usual way when outside the loop. Just type @products[2] not sure how to answer your second question. bruce On 12-Dec-05, at 9:19 PM, charlie bowman wrote:> I''m tranforming my catalyst app over to a rails app and I''m stumped > again with the template. I guess it''s as much of a ruby question as a > rails question but here goes. I know i can say <% for product in > products %> <%= product.size %> <%end%> but how can I reference a > specific element of products outside of the loop? Also how can I > determine which element I''m on in the loop? I keep trying to do it > in a > perlish way which of course breaks. I''ve searched but I can''t find an > answere. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
bruce balmer wrote:> <newbie warning> I am one, so take all I say with a pinch of salt </ > warning> > > if you have an array called @products then you can access the third > element the usual way when outside the loop. Just type @products[2] > > not sure how to answer your second question. > > bruceAs it turns out I think I have an array of hashes. @products has man products and each of them have a size, shape, price etc. I can access each of them in the loop but I''m trying to create a table and I want to end, then start a table row each 5 items. If I know which iteration I''m on in the loop then I code for the new <tr> Back to the first question, how to I access product.size of the first array element? -- Posted via http://www.ruby-forum.com/.
array.each_with_index do |item, index| "<br>Item #{index} is #{item}" end Array is zero-based. Second item is array[1] Rich Clingman ----- Original Message ----- From: "charlie bowman" <cbowmanschool-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Monday, December 12, 2005 8:19 PM Subject: [Rails] templates and arrays> I''m tranforming my catalyst app over to a rails app and I''m stumped > again with the template. I guess it''s as much of a ruby question as a > rails question but here goes. I know i can say <% for product in > products %> <%= product.size %> <%end%> but how can I reference a > specific element of products outside of the loop? Also how can I > determine which element I''m on in the loop? I keep trying to do it in a > perlish way which of course breaks. I''ve searched but I can''t find an > answere. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
charlie bowman wrote:> As it turns out I think I have an array of hashes.Technically you have an array of product objects. Each of those objects will act like a Hash, but they are more complicated than that.> @products has many products and each of them have a size, shape, price etc. > I can access each of them in the loop but I''m trying to create a table and I > want to end, then start a table row each 5 items. If I know which iteration > I''m on in the loop then I code for the new <tr>Hmm... what you are trying to do is pretty simple, but knowing which loop iteration isn''t exactly what you asked in your first email. You can either use the products.each_with_index looping method, which gives you the index of the current product object. Add 1 to that number and % 5. If you get 0, then you are on an "every 5th".> Back to the first question, how to I access product.size of the first > array element?Given the products array, just do products[0].size. -Brian
<%= product[0].size %> produces this error You have a nil object when you didn''t expect it! The error occured while evaluating nil.size Rich Clingman wrote:> array.each_with_index do |item, index| > "<br>Item #{index} is #{item}" > end > > Array is zero-based. > > Second item is > array[1] > > Rich Clingman > > > ----- Original Message ----- > From: "charlie bowman" <cbowmanschool-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Sent: Monday, December 12, 2005 8:19 PM > Subject: [Rails] templates and arrays-- Posted via http://www.ruby-forum.com/.
> <%= product[0].size %> produces this error > > You have a nil object when you didn''t expect it! > The error occured while evaluating nil.sizeSorry, I don''t know what to tell you... product[0] is not initiated. Could it be @product[0]? No idea since I don''t know your code. Try <%= debug array %> and <%= debug @array %> to see what''s initiated in those vars. To see example of debug dump, put <%= debug request %> in your template or "view" Rich Clingman ----- Original Message ----- From: "charlie bowman" <cbowmanschool-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Monday, December 12, 2005 9:51 PM Subject: [Rails] Re: templates and arrays> > <%= product[0].size %> produces this error > > You have a nil object when you didn''t expect it! > The error occured while evaluating nil.size > > > > Rich Clingman wrote: >> array.each_with_index do |item, index| >> "<br>Item #{index} is #{item}" >> end >> >> Array is zero-based. >> >> Second item is >> array[1] >> >> Rich Clingman >> >> >> ----- Original Message ----- >> From: "charlie bowman" <cbowmanschool-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> >> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >> Sent: Monday, December 12, 2005 8:19 PM >> Subject: [Rails] templates and arrays > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
only instance variables are "magically" passed from the controller to the view. So if you have product in your controller and you try to use product in your view - it won''t work. I had the same experience myself. but @product in the controller and the view WILL work. I don''t understand why. bruce On 12-Dec-05, at 11:01 PM, Rich Clingman wrote:>> <%= product[0].size %> produces this error >> You have a nil object when you didn''t expect it! >> The error occured while evaluating nil.size > > Sorry, I don''t know what to tell you... product[0] is not initiated. > Could it be @product[0]? > No idea since I don''t know your code. > > Try <%= debug array %> > and <%= debug @array %> > > to see what''s initiated in those vars. > > To see example of debug dump, put > <%= debug request %> > in your template or "view" > > Rich Clingman > > > ----- Original Message ----- From: "charlie bowman" > <cbowmanschool-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Sent: Monday, December 12, 2005 9:51 PM > Subject: [Rails] Re: templates and arrays > > >> <%= product[0].size %> produces this error >> You have a nil object when you didn''t expect it! >> The error occured while evaluating nil.size >> Rich Clingman wrote: >>> array.each_with_index do |item, index| >>> "<br>Item #{index} is #{item}" >>> end >>> Array is zero-based. >>> Second item is >>> array[1] >>> Rich Clingman >>> ----- Original Message ----- >>> From: "charlie bowman" <cbowmanschool-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> >>> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>> Sent: Monday, December 12, 2005 8:19 PM >>> Subject: [Rails] templates and arrays >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Charlie: I think you have had an answer to this already. I agree with that person. To have a table open and close every 5 rows does not require you knowing where in the array of @product objects you are. Just set a counter and use modulo and if you find modulo confusing (many people do) then just use two loops one of which counts indefinitely and one which counts to find and then is reset using a simple if statement. Modulo is definitely more efficient. bruce On 12-Dec-05, at 10:38 PM, charlie bowman wrote:> bruce balmer wrote: >> <newbie warning> I am one, so take all I say with a pinch of salt </ >> warning> >> >> if you have an array called @products then you can access the third >> element the usual way when outside the loop. Just type @products[2] >> >> not sure how to answer your second question. >> >> bruce > > As it turns out I think I have an array of hashes. @products has man > products and each of them have a size, shape, price etc. I can access > each of them in the loop but I''m trying to create a table and I > want to > end, then start a table row each 5 items. If I know which > iteration I''m > on in the loop then I code for the new <tr> > Back to the first question, how to I access product.size of the first > array element? > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
charlie bowman wrote:> <%= product[0].size %> produces this error > > You have a nil object when you didn''t expect it! > The error occured while evaluating nil.sizeYes. And there are two reasons why this would cause a nil error. First, it could be the scope of the variable itself. Like Rich mentioned, you need a variable in the instance scope (one beginning with an @) to have it automatically carry over from the controller to the view. So, if product was an array (it''s not, see below) and you defined it in the controller, you would need to name it @product if you wanted to access its values in the view. Second, you have a typo. It is products that is the array, not product. The singular variable was the one that was being set to each element in the pluralized variable array, each time through the loop. So product is never an array, and therefore can never have a [0] element. -Brian
Bruce Balmer wrote:> only instance variables are "magically" passed from the controller to > the view. So if you have product in your controller and you try to use > product in your view - it won''t work. I had the same experience myself.Just to try and clear this up, a bit, there''s nothing magical about instance variables. They simply have a wider scope than a local variable. If it helps, think of instance variables as global variables that exist for a single HTTP request. Once they are defined, during the request, they are available to all subsequent code snippets that need to access them. If you define them in the controller (which is probably best, since the controller is called very early on) they are then usable by the main view, all partial views, and any helpers those views need to call.> but @product in the controller and the view WILL work. I don''t > understand why.Define a variable @product in the controller and the view is able to access the same variable, yes. -Brian
I do have access to @array in the view. I just can''t access one element of it out of the loop Here is my view. the for loop works perfectly but the products[0].size produces errors. Why does the loop work but not accessing the single element? Thanks for your help...I''m very new to ruby and rails <% for product in @products %> <td> <a href="javascript: Load_Details(''<%= product.image_url %>''.''<%= product.title %>'',''<%= product.description %>'',''<%= product.size %>'',''<%= product.price %>'',''<%= product.id %>'');"> <img src="<%= product.image_url %>" class="small_product" onMouseOver="this.className = ''product_hover''" onMouseOut="this.className = ''small_product''"></a> </td> </tr> <tr> <% end %> <%= products[0].size %> Brian V. Hughes wrote:> Bruce Balmer wrote: >> only instance variables are "magically" passed from the controller to >> the view. So if you have product in your controller and you try to use >> product in your view - it won''t work. I had the same experience myself. > > Just to try and clear this up, a bit, there''s nothing magical about > instance > variables. They simply have a wider scope than a local variable. If it > helps, > think of instance variables as global variables that exist for a single > HTTP > request. Once they are defined, during the request, they are available > to all > subsequent code snippets that need to access them. If you define them in > the > controller (which is probably best, since the controller is called very > early > on) they are then usable by the main view, all partial views, and any > helpers > those views need to call. > >> but @product in the controller and the view WILL work. I don''t >> understand why. > > Define a variable @product in the controller and the view is able to > access the > same variable, yes. > > -Brian-- Posted via http://www.ruby-forum.com/.
charlie bowman wrote:> I do have access to @array in the view. I just can''t access one element > of it out of the loop Here is my view. the for loop works perfectly but > the products[0].size produces errors. Why does the loop work but not > accessing the single element? Thanks for your help...I''m very new to > ruby and rails > > <% for product in @products %> > > <%= products[0].size %>Because you aren''t referring to the proper object. You need to ask for @products[0].size, since @products is the array of product objects that you created in your controller. -Brian
On Dec 13, 2005, at 4:58 PM, charlie bowman wrote:> I do have access to @array in the view. I just can''t access one > element > of it out of the loop Here is my view. the for loop works > perfectly but > the products[0].size produces errors. Why does the loop work but not > accessing the single element? Thanks for your help...I''m very new to > ruby and rails > > <% for product in @products %> > > <td> > <a href="javascript: Load_Details(''<%= > product.image_url > %>''.''<%= product.title %>'',''<%= product.description %>'',''<%> product.size %>'',''<%= product.price %>'',''<%= product.id %>'');"> <img > src="<%= product.image_url %>" class="small_product" > onMouseOver="this.className = ''product_hover''" > onMouseOut="this.className = ''small_product''"></a> > </td> > </tr> > <tr> > <% end %> > > <%= products[0].size %><%= @products[0].size %>> > > > Brian V. Hughes wrote: >> Bruce Balmer wrote: >>> only instance variables are "magically" passed from the >>> controller to >>> the view. So if you have product in your controller and you try >>> to use >>> product in your view - it won''t work. I had the same experience >>> myself. >> >> Just to try and clear this up, a bit, there''s nothing magical about >> instance >> variables. They simply have a wider scope than a local variable. >> If it >> helps, >> think of instance variables as global variables that exist for a >> single >> HTTP >> request. Once they are defined, during the request, they are >> available >> to all >> subsequent code snippets that need to access them. If you define >> them in >> the >> controller (which is probably best, since the controller is called >> very >> early >> on) they are then usable by the main view, all partial views, and any >> helpers >> those views need to call. >> >>> but @product in the controller and the view WILL work. I don''t >>> understand why. >> >> Define a variable @product in the controller and the view is able to >> access the >> same variable, yes. >> >> -Brian > > > -- > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Thank you guys! I did need the @ in front of it. I''m trying to learn ruby by goind throught the rails book...and it''s pretty hard. Brian V. Hughes wrote:> charlie bowman wrote: >> I do have access to @array in the view. I just can''t access one element >> of it out of the loop Here is my view. the for loop works perfectly but >> the products[0].size produces errors. Why does the loop work but not >> accessing the single element? Thanks for your help...I''m very new to >> ruby and rails >> >> <% for product in @products %> >> >> <%= products[0].size %> > > > Because you aren''t referring to the proper object. You need to ask for > @products[0].size, since @products is the array of product objects that > you > created in your controller. > > -Brian-- Posted via http://www.ruby-forum.com/.
Brian: Thanks. Your explanation really cleared up something that has been mystifying me. bruce On 13-Dec-05, at 7:23 PM, Ezra Zygmuntowicz wrote:>> Brian V. Hughes wrote: >>> Bruce Balmer wrote: >>>> only instance variables are "magically" passed from the >>>> controller to >>>> the view. So if you have product in your controller and you try >>>> to use >>>> product in your view - it won''t work. I had the same experience >>>> myself. >>> >>> Just to try and clear this up, a bit, there''s nothing magical about >>> instance >>> variables. They simply have a wider scope than a local variable. >>> If it >>> helps, >>> think of instance variables as global variables that exist for a >>> single >>> HTTP >>> request. Once they are defined, during the request, they are >>> available >>> to all >>> subsequent code snippets that need to access them. If you define >>> them in >>> the >>> controller (which is probably best, since the controller is >>> called very >>> early >>> on) they are then usable by the main view, all partial views, and >>> any >>> helpers >>> those views need to call. >>> >>>> but @product in the controller and the view WILL work. I don''t >>>> understand why. >>> >>> Define a variable @product in the controller and the view is able to >>> access the >>> same variable, yes. >>> >>> -Brian >>_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Tom Fakes wrote:> The best way to do this is: > > <% products.each_with_index { |product, index| %> > <%= product.size %> > <% } %> >I would use the do/end syntax in place of the { } since you are having it span multiple lines, and since embedded in html. It is somewhat ambiguous because hashes can be created with {}. <% products.each_width_index do |product,index| %> <%= product.size %> <% end %> I think ''end'' in embedded in HTML also gives a better visual for way a code block stops in ruby. Zach
> > Thank you guys! I did need the @ in front of it. I''m trying to learn > ruby by goind throught the rails book...and it''s pretty hard.One thing you can do to learn (a lot) more is to write samples/snippets in the form of unit tests. This way you can experiment with the various ruby constructs, in a small and well defined scope. It''s rather easy to write unit tests for Ruby (see http://www.rubygarden.org/ruby?TestUnit). I''ve already learned many things this way! --Thibaut _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails