Templates and arrays


#1

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.


#2

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


#3

bruce balmer wrote:

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


Back to the first question, how to I access product.size of the first
array element?

#4

array.each_with_index do |item, index|

Item #{index} is #{item}”
end

Array is zero-based.

Second item is
array[1]

Rich C.

----- Original Message -----
From: “charlie bowman” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Monday, December 12, 2005 8:19 PM
Subject: [Rails] templates and arrays


#5

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

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


#6

<%= 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 C. wrote:

array.each_with_index do |item, index|

Item #{index} is #{item}”
end

Array is zero-based.

Second item is
array[1]

Rich C.

----- Original Message -----
From: “charlie bowman” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Monday, December 12, 2005 8:19 PM
Subject: [Rails] templates and arrays


#7

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


#8

<%= 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 C.

----- Original Message -----
From: “charlie bowman” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Monday, December 12, 2005 9:51 PM
Subject: [Rails] Re: templates and arrays


#9

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


#10

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.size

Yes. 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


#11

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 %>’);">



<% end %>
    <%= products[0].size %>

Brian V. Hughes wrote:

Bruce B. 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


#12

Bruce B. 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


#13

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


#14

On Dec 13, 2005, at 4:58 PM, charlie bowman wrote:

        <td>

<% end %>

    <%= products[0].size %>
  <%= @products[0].size %>

myself.
available


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#15

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


#16

Brian:

Thanks. Your explanation really cleared up something that has been
mystifying me.

bruce


#17

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