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 -- Posted via http://www.ruby-forum.com/.
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 On 4/17/06, Jan <shocktone@lycos.de> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- John Hornbeck -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060417/0704f9b2/attachment.html
If you write out the loop in long hand it makes more sense. for each item in products array item dot title next item If you know the id in particular you want to show you can do item[1].title otherwise you have to loop through them. --------- Original Message -------- From: rails@lists.rubyonrails.org To: rails@lists.rubyonrails.org <rails@lists.rubyonrails.org> Subject: Re: [Rails] ruby on rails book, fundamental question Date: 17/04/06 17:58> 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?> JohnOn 4/17/06, Jan <shocktone@lycos.de> wrote: > Hi guys,bought the ROR book. I understand most of it and really like it.Thereis just on thing I just dont get.Lets take this code for example.def list @products = paginate :product, :per_page => 10> endAs far as I understand @products should be some kinda hash orarray,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.Thanksalot--Posted via http://www.ruby-forum.com/._______________________________________________> Rails mailinglistRails@lists.rubyonrails.orghttp://lists.rubyonrails.org/mailman/listinfo/rails> -- John Hornbeck > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 Hornbeck 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-- Posted via http://www.ruby-forum.com/.
I forgot. Is there a command in ruby to output the whole array ? Just for me to check ? -- Posted via http://www.ruby-forum.com/.
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 On 4/17/06, Jan <shocktone@lycos.de> wrote:> > I forgot. Is there a command in ruby to output the whole array ? > Just for me to check ? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- John Hornbeck -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060417/90afddb9/attachment-0001.html
Douglas Livingstone
2006-Apr-17 13:44 UTC
[Rails] Re: ruby on rails book, fundamental question
2006/4/17, Jan <shocktone@lycos.de>:> 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
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 On 17 Apr 2006, at 16:43, Douglas Livingstone wrote:> 2006/4/17, Jan <shocktone@lycos.de>: >> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Douglas, Thanks for the inspect command. I had never seen that, it will be real helpful now. On 4/17/06, John Evans <junk.3eyes@gmail.com> wrote:> > 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 > > > On 17 Apr 2006, at 16:43, Douglas Livingstone wrote: > > > 2006/4/17, Jan <shocktone@lycos.de>: > >> 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 > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- John Hornbeck -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060417/99bea8af/attachment-0001.html
@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:> 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 > >-- Sau Sheong http://blog.saush.com http://read.saush.com http://jaccal.sourceforge.net