Forum: Ruby on Rails Stupid? question about if else logic

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.
Jason P. (Guest)
on 2006-05-12 09:28
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?
Daniel -. (Guest)
on 2006-05-12 09:46
(Received via mailing list)
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.
Jason P. (Guest)
on 2006-05-12 10:08
Thanks, I thought for some reason I was missing an obvious easy way as
is my tendency.
Jason P. (Guest)
on 2006-05-12 10:43
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:
Claus G. (Guest)
on 2006-05-12 10:47
(Received via mailing list)
> 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>

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

> 5:    <p>Title: <%=h @image.title%></p>
> 6:    <%= image_tag(url_for_file_column 'image','file')%>
> 7: <% end %>
> 8:

regards
Claus
Daniel -. (Guest)
on 2006-05-12 10:50
(Received via mailing list)
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 %>
Jason P. (Guest)
on 2006-05-12 10:57
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
Daniel -. (Guest)
on 2006-05-12 11:06
(Received via mailing list)
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 %>
Jason P. (Guest)
on 2006-05-12 11:13
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.
Jason P. (Guest)
on 2006-05-12 11:18
Also I should mention:  I started working on this from this tutorial:
http://www.fearoffish.com/articles/2006/04/26/file...

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.
Chris T (Guest)
on 2006-05-12 11:37
(Received via mailing list)
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.
Daniel -. (Guest)
on 2006-05-12 11:58
(Received via mailing list)
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
This topic is locked and can not be replied to.