this seems so stupid, I keep thinking I''ve dealt with this before but I have this code <h1>Portraits</h1> <% if @image.portrait == 1 %> <%= image_tag(url_for_file_column ''image'', ''file'', ''thumb'') %> <% else %> <h1>Landscapes</h1> <%= image_tag(url_for_file_column ''image'', ''file'', ''thumb'') %> <% end %> where I want the landscapes heading only to show once. I don''t want to iterate through the series twice (DRY) so isn''t there an obvious way to accomplish this? -- Posted via http://www.ruby-forum.com/.
I think you have to break it into two differnt parts. I don''t think you can do what your after with one if statement. Assuming that you have an initial varaible containing many images @images.. #portraits @portraits = @images.collect { |i| i.portrait ==1 } #landscapes @landscapes = @images.collect{ |i| i.portrait != 1} <h1>Portraits</h1> @portraits.each do |image| link stuff in here end <h1>Landscapes</h1> @landscapes.each do |image| link stuff in here end or I think you can collect inline ie <h1>Portraits</h1> @images.collect{|i| i.portrait == 1 }.each do |image| link stuff in here end <h1>Landscapes</h1> @images.collect{|i| i.portrait != 1 }.each do |image| link stuff in here end Or you could write a helper to do this for you. On 5/12/06, Jason Pfeifer <jpfeifer@shaw.ca> wrote:> > this seems so stupid, I keep thinking I''ve dealt with this before but I > have this code > > <h1>Portraits</h1> > <% if @image.portrait == 1 %> > <%= image_tag(url_for_file_column ''image'', ''file'', ''thumb'') %> > <% else %> > <h1>Landscapes</h1> > <%= image_tag(url_for_file_column ''image'', ''file'', ''thumb'') %> > <% end %> > > where I want the landscapes heading only to show once. I don''t want to > iterate through the series twice (DRY) so isn''t there an obvious way to > accomplish this? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060512/12a376b2/attachment.html
Thanks, I thought for some reason I was missing an obvious easy way as is my tendency. -- Posted via http://www.ruby-forum.com/.
using this in my controller: @portraits = @images.collect { |i| i.portrait ==1 } and this in my view: <% @portraits.each do |@image| %> <p>Title: <%=h @image.title%></p> <%= image_tag(url_for_file_column ''image'',''file'')%> <% end %> I get the error: undefined method `title'' for #<Array:0x3809f48> Extracted source (around line #5): 2: <h1>Portraits</h1> 3: 4: <% @portraits.each do |@image| %> 5: <p>Title: <%=h @image.title%></p> 6: <%= image_tag(url_for_file_column ''image'',''file'')%> 7: <% end %> 8: -- Posted via http://www.ruby-forum.com/.
> using this in my controller: > > @portraits = @images.collect { |i| i.portrait ==1 } > > and this in my view: > > <% @portraits.each do |@image| %>--------------------------------------^^^ Remove the @-sign.> <p>Title: <%=h @image.title%></p>-----------------------------^^^> <%= image_tag(url_for_file_column ''image'',''file'')%> > <% end %> > > I get the error: > undefined method `title'' for #<Array:0x3809f48> > Extracted source (around line #5): > > 2: <h1>Portraits</h1> > 3: > 4: <% @portraits.each do |@image| %> > 5: <p>Title: <%=h @image.title%></p> > 6: <%= image_tag(url_for_file_column ''image'',''file'')%> > 7: <% end %> > 8:regards Claus
What output do you get if you do an @images.inspect Also in your extracted source you should not use an instance variable inside your block. Should be: 2: <h1>Portraits</h1> 3: 4: <% @portraits.each do |image| %> 5: <p>Title: <%=h image.title%></p> 6: <%= image_tag(url_for_file_column ''image'',''file'')%> 7: <% end %> On 5/12/06, Jason Pfeifer <jpfeifer@shaw.ca> wrote:> > using this in my controller: > > @portraits = @images.collect { |i| i.portrait ==1 } > > and this in my view: > > <% @portraits.each do |@image| %> > <p>Title: <%=h @image.title%></p> > <%= image_tag(url_for_file_column ''image'',''file'')%> > <% end %> > > I get the error: > > undefined method `title'' for #<Array:0x3809f48> > > Extracted source (around line #5): > > 2: <h1>Portraits</h1> > 3: > 4: <% @portraits.each do |@image| %> > 5: <p>Title: <%=h @image.title%></p> > 6: <%= image_tag(url_for_file_column ''image'',''file'')%> > 7: <% end %> > 8: > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060512/20d6b37b/attachment.html
I should have mentioned I''d tried that already still get the error undefined method `title'' for true:TrueClass I do @images.inspect and get: [#"portrait", "id"=>"14", "portrait"=>"1", "file"=>"All_Shades_of_Blue.jpg"}>, #"landscape", "id"=>"16", "portrait"=>"0", "file"=>"Calgary-Highway-Driving.jpg"}>, #"another landscape", "id"=>"17", "portrait"=>"0", "file"=>"bus_on_seymour.jpg"}>, #"newest image", "id"=>"18", "portrait"=>"1", "file"=>"A_Plant_Personified.jpg"}>, #"newer image", "id"=>"19", "portrait"=>"1", "file"=>"A_Plant_Personified.jpg"}>] which are just images that I''ve uploaded and tested around with -- Posted via http://www.ruby-forum.com/.
There is no title attribute for the objects in the @images variables. It seems that there is an object within each object tho. What do you get if you print images class instead of title? 2: <h1>Portraits</h1> 3: 4: <% @portraits.each do |image| %> 5: <p>Title: <%=h image.class %></p> 6: <%= image_tag(url_for_file_column ''image'',''file'')%> 7: <% end %> On 5/12/06, Jason Pfeifer <jpfeifer@shaw.ca> wrote:> > I should have mentioned I''d tried that already > > still get the error > > undefined method `title'' for true:TrueClass > > I do @images.inspect and get: > > [#"portrait", "id"=>"14", "portrait"=>"1", > "file"=>"All_Shades_of_Blue.jpg"}>, #"landscape", "id"=>"16", > "portrait"=>"0", "file"=>"Calgary-Highway-Driving.jpg"}>, #"another > landscape", "id"=>"17", "portrait"=>"0", "file"=>"bus_on_seymour.jpg"}>, > #"newest image", "id"=>"18", "portrait"=>"1", > "file"=>"A_Plant_Personified.jpg"}>, #"newer image", "id"=>"19", > "portrait"=>"1", "file"=>"A_Plant_Personified.jpg"}>] > > which are just images that I''ve uploaded and tested around with > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060512/16e22f2a/attachment.html
Jason Pfeifer
2006-May-12 07:13 UTC
[Rails] Re: Re: Re: Stupid? question about if else logic
Daniel ----- wrote:> There is no title attribute for the objects in the @images variables. > > It seems that there is an object within each object tho. > > What do you get if you print images class instead of title?I get FalseClass back from that. I noticed there is no title attribute in there as well, which is definitely strange to me because I''m using MySQL front and I can see the column name, and the data for it in each row. -- Posted via http://www.ruby-forum.com/.
Jason Pfeifer
2006-May-12 07:18 UTC
[Rails] Re: Re: Re: Stupid? question about if else logic
Also I should mention: I started working on this from this tutorial: http://www.fearoffish.com/articles/2006/04/26/file_column-and-lightbox and this code from there totally works for me with no problems: <% @images.each do |@image| %> <p>Title: <%=h @image.title %></p> <%= image_tag(url_for_file_column ''image'', ''file'') %><br /> <% end %> but if I take out the @ in that code, it doesn''t work for me anymore. -- Posted via http://www.ruby-forum.com/.
Seem to remember there''s something about file_column that requires an instance variable in iterations (when you''d normally use a local variable). Think there''s something on the wiki about it. Jason Pfeifer wrote:> Also I should mention: I started working on this from this tutorial: > http://www.fearoffish.com/articles/2006/04/26/file_column-and-lightbox > > and this code from there totally works for me with no problems: > > <% @images.each do |@image| %> > <p>Title: <%=h @image.title %></p> > <%= image_tag(url_for_file_column ''image'', ''file'') %><br /> > <% end %> > > but if I take out the @ in that code, it doesn''t work for me anymore. > >
Hi Jason, I''ve gotten home from work and been able to try along with the tutorial. Thanx for listing it. in the url_for_file_column call that you have instead of ''image'' put the object image. The problem is in the url_for_file_column def url_for_file_column(object, method, options=nil) case object when String, Symbol object = instance_variable_get("@#{object.to_s}") end ... You can see that when you supply a string, it tries to find an instance variable of that name. Hence when you took the @ away, you also took away it''s status as an instance variable. If instead you just use the object, then there is no problem. You bypass the call to instance method all together. I think there is something not quite right about using instance variables like this in a block. I''d stick with using url_for_file_column with an object. Cheers On 5/12/06, Jason Pfeifer <jpfeifer@shaw.ca> wrote:> > Also I should mention: I started working on this from this tutorial: > http://www.fearoffish.com/articles/2006/04/26/file_column-and-lightbox > > and this code from there totally works for me with no problems: > > <% @images.each do |@image| %> > <p>Title: <%=h @image.title %></p> > <%= image_tag(url_for_file_column ''image'', ''file'') %><br /> > <% end %> > > but if I take out the @ in that code, it doesn''t work for me anymore. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060512/44c5c890/attachment.html