Forum: Ruby on Rails ruby on rails book, fundamental question

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.
Jan (Guest)
on 2006-04-17 16:49
Hi guys,

bought the ROR book. I understand most of it and really like it. There
is just on thing I just dont get.
Lets take this code for example.

def list
   @products = paginate :product, :per_page => 10
end

As far as I understand @products should be some kinda hash or array,
with the data of the Database inside.
How can I display this array ?

One solution is to do it like this (like in the book)

<% for product in @products -%>
        <img src="<%= product.image_url %>"/>
<% end %>

But I want to understand (!) how this works. Something like puts
@products["title"] or puts @products[:title] which obviously doesnt
work.

I think this would help to understand the whole concept.

Thanks alot
John H. (Guest)
on 2006-04-17 16:58
(Received via mailing list)
I will try to explain this.  If you do the same command from
./script/console "@products = Product.find(:all)", you will be able to
access this hash by calling each element as @products[1].title or by
other
keys in the hash.  Does that make since?

John
Jan (Guest)
on 2006-04-17 17:04
Hi John,

I just found this out myself about 1 Minute ago.
Totally makes sense. Makes things much clearer.

I have one more question though

In the rhtml file you find something like this
<%= link_to 'Show', :action => 'show', :id => product %><br/>

in a controller you find:

def show
    @product = Product.find(@params[:id])
end

Everything makes sense. Im just not shure where the @params comes from.
Is this some part of the framework ?

Thanks alot again... ;)


John H. wrote:
> I will try to explain this.  If you do the same command from
> ./script/console "@products = Product.find(:all)", you will be able to
> access this hash by calling each element as @products[1].title or by
> other
> keys in the hash.  Does that make since?
>
> John
Jan (Guest)
on 2006-04-17 17:06
I forgot. Is there a command in ruby to output the whole array ?
Just for me to check ?
John H. (Guest)
on 2006-04-17 17:20
(Received via mailing list)
The @params would be the parameter's that you pass with the :id =>
product
or if you pass other params as well.  On the other question the only way
that I know how to print a whole array is with the @products.each  or
for
product in @products.  Hope that helps,

John
Douglas L. (Guest)
on 2006-04-17 17:44
(Received via mailing list)
2006/4/17, Jan <removed_email_address@domain.invalid>:
> I forgot. Is there a command in ruby to output the whole array ?
> Just for me to check ?
>

Try my_array.inspect, that should do what you're after.

hth,
Douglas
John Evans (Guest)
on 2006-04-17 18:37
(Received via mailing list)
Also of interest might be

logger.info @product

which will write out the array to the relevant log file

or something like

p @product

will print out the array (regardless of if the content is a string or
not) to the console running webrick
John H. (Guest)
on 2006-04-17 18:49
(Received via mailing list)
Douglas,

Thanks for the inspect command.  I had never seen that, it will be real
helpful now.
Chang Sau S. (Guest)
on 2006-04-17 19:39
(Received via mailing list)
@products is an array of Product objects. In your code you are iterating
through the array and for each Product object you are accessing the
attribute image_url.

@products["title"] would be trying to access an array with a string as
the index, which doesn't work (this would work with a hash though).

Jan wrote:
> As far as I understand @products should be some kinda hash or array,
> @products["title"] or puts @products[:title] which obviously doesnt
> work.
>
> I think this would help to understand the whole concept.
>
> Thanks alot
>
>


--
Sau S.

http://blog.saush.com
http://read.saush.com
http://jaccal.sourceforge.net
This topic is locked and can not be replied to.