Stupid? question about if else logic


#1

this seems so stupid, I keep thinking I’ve dealt with this before but I
have this code

Portraits

<% if @image.portrait == 1 %> <%= image_tag(url_for_file_column 'image', 'file', 'thumb') %> <% else %>

Landscapes

<%= 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?


#2

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}

Portraits

@portraits.each do |image| link stuff in here end

Landscapes

@landscapes.each do |image| link stuff in here end

or I think you can collect inline
ie

Portraits

@images.collect{|i| i.portrait == 1 }.each do |image| link stuff in here end

Landscapes

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


#3

Thanks, I thought for some reason I was missing an obvious easy way as
is my tendency.


#4

using this in my controller:

@portraits = @images.collect { |i| i.portrait ==1 }

and this in my view:

<% @portraits.each do |@image| %>

Title: <%=h @image.title%>

<%= 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:

Portraits


3:
4: <% @portraits.each do |@image| %>
5:

Title: <%=h @image.title%>


6: <%= image_tag(url_for_file_column ‘image’,‘file’)%>
7: <% end %>
8:

#5

using this in my controller:

@portraits = @images.collect { |i| i.portrait ==1 }

and this in my view:

<% @portraits.each do |@image| %>

--------------------------------------^^^

Remove the @-sign.

Title: <%=h @image.title%>

-----------------------------^^^

5:

Title: <%=h @image.title%>


6: <%= image_tag(url_for_file_column ‘image’,‘file’)%>
7: <% end %>
8:

regards
Claus


#6

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


#7

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:

Portraits


3:
4: <% @portraits.each do |image| %>
5:

Title: <%=h image.title%>


6: <%= image_tag(url_for_file_column ‘image’,‘file’)%>
7: <% end %>

#8

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.


#9

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:

Portraits


3:
4: <% @portraits.each do |image| %>
5:

Title: <%=h image.class %>


6: <%= image_tag(url_for_file_column ‘image’,‘file’)%>
7: <% end %>

#10

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.


#11

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| %>

Title: <%=h @image.title %>


<%= image_tag(url_for_file_column ‘image’, ‘file’) %>

<% end %>

but if I take out the @ in that code, it doesn’t work for me anymore.


#12

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