Forum: Ruby on Rails templates and arrays

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.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-13 05:19
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.
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2005-12-13 06:15
(Received via mailing list)
<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
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-13 06:38
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?
F8903dc7d2ddc733b08e0fa745b07fe1?d=identicon&s=25 Rich Clingman (Guest)
on 2005-12-13 06:48
(Received via mailing list)
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@yahoo.com>
To: <rails@lists.rubyonrails.org>
Sent: Monday, December 12, 2005 8:19 PM
Subject: [Rails] templates and arrays
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2005-12-13 06:51
(Received via mailing list)
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
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-13 06:51
<%= 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@yahoo.com>
> To: <rails@lists.rubyonrails.org>
> Sent: Monday, December 12, 2005 8:19 PM
> Subject: [Rails] templates and arrays
F8903dc7d2ddc733b08e0fa745b07fe1?d=identicon&s=25 Rich Clingman (Guest)
on 2005-12-13 07:03
(Received via mailing list)
> <%= 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@yahoo.com>
To: <rails@lists.rubyonrails.org>
Sent: Monday, December 12, 2005 9:51 PM
Subject: [Rails] Re: templates and arrays
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2005-12-13 14:49
(Received via mailing list)
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
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2005-12-13 14:55
(Received via mailing list)
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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2005-12-13 15:28
(Received via mailing list)
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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2005-12-13 15:34
(Received via mailing list)
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
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-14 01:58
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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2005-12-14 03:01
(Received via mailing list)
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
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-14 03:26
(Received via mailing list)
On Dec 13, 2005, at 4:58 PM, charlie bowman wrote:

>             <td>
> <% end %>
>
>         <%= products[0].size %>
      <%= @products[0].size %>
>>> myself.
>> available
>>
> --
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2005-12-14 03:34
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
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2005-12-14 15:03
(Received via mailing list)
Brian:

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

bruce
91eb330fb36d1e03c856574dfb77d2bc?d=identicon&s=25 =?ISO-8859-1?Q?Thibaut_Barr=E8re?= (Guest)
on 2005-12-14 16:00
(Received via mailing list)
>
> 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
This topic is locked and can not be replied to.