Topic: Images with for loops

I guess a bit more Ruby than Rails,

So I have four columns in my table called img0, img1, img2, and img3. The columns may or may not contain an actual img.

I would like to have a for loop in my view that will check first to see if the field is nil and if not %= the filename.


My pseudo-code:

@num = 0
for num < 3
     item.img[num].nil?
     if true
        image_tag(item.img[num])
        num + 1
     else
        num + 1
     end
end

Am I on the right track? Is there a better way to do this? Should I put this into a controller as a method?

Last edited by kidhero (2006-11-29 11:09:53)

Re: Images with for loops

You should move the four image columns into a separate images table and add a one-to-many association to the original table (has_many/belongs_to). You could then easily loop through the images like this:

<% for image in @some_model.images %>
  <%= image.path %>
<% end %>

Railscasts - Free Ruby on Rails Screencasts

Re: Images with for loops

ryanb wrote:

You should move the four image columns into a separate images table and add a one-to-many association to the original table (has_many/belongs_to). You could then easily loop through the images like this:

<% for image in @some_model.images %>
  <%= image.path %>
<% end %>

thanks ryanb.

is there a way that i can put in a method in the model that searches in the columns for the pics and then store that into an array?

Re: Images with for loops

You mean with your current schema (img0, img1, etc.)? You can try this:

# in manager
def images
  images = []
  attributes.each do |column, value|
    if column =~ /^img/
      images << value
    end
  end
  images
end

I still highly recommend creating a separate Image model.

Edit: Here's a prettier way:

def images
  attributes.reject { |column, value| column !~ /^img/ }.values
end

Last edited by ryanb (2006-11-29 15:50:07)

Railscasts - Free Ruby on Rails Screencasts

Re: Images with for loops

ryanb,

i'm trying a couple solutions before putting up another table. i've read your multiple model in one form tutorial but it's too much coding.

my other temp solution is use a nil? check with an image_tag, but the problem is then that i can't modify the default image_tag path.

so:

if item.img0.nil? != nil then puts image_tag(item.img0) end

Re: Images with for loops

kidhero wrote:

my other temp solution is use a nil? check with an image_tag, but the problem is then that i can't modify the default image_tag path.

I'm not sure what you mean by this. You can prefix the img0 value with a full path to override the default one supplied by image_tag.

Railscasts - Free Ruby on Rails Screencasts

Re: Images with for loops

when i do image_tag(img0) it stubbornly continues to output it in html as /images/img0path or if i don't set anything it calls it up as a png file. i haven't found a way to modify the path... i'll look in the API docs more carefully

Re: Images with for loops

Have you tried something like this?

image_tag('/my_images/' + img0)

That should, from what the docs say, change the default /images/ path to /my_images/

Railscasts - Free Ruby on Rails Screencasts

Re: Images with for loops

lol thank you so much ryanb, that worked, kind of.

the html that's outputted:

            <img alt="" src="/images/../images/assets/.png?" />
            <img alt="Card1-front" src="/images/../images/assets/card1-front.jpg?1163466978" />
            <img alt="Card1-back" src="/images/../images/assets/card1-back.jpg?1163466988" />
            <img alt="" src="/images/../images/assets/.png?" />
            <img alt="" src="/images/../images/assets/.png?" />

still a little bit of problems but it's still displaying the right pictures at the right places. i spent a good ten minutes putting in every possible combination of apostrophes and plus signs and variables. i bow down at your ingenuity.




the if nil check works quite well. in an effort to DRY up my code is there a way to loop this?

the array is item.img_0 and item.img_1 and item.img_2

can i set up a variable to have it run through and replace the numbers?

Last edited by kidhero (2006-11-29 19:36:48)

Re: Images with for loops

Not sure what you mean. Does the code I gave above do what you want?

# in model
def images
  attributes.reject { |column, value| column !~ /^img/ }.values
end

You can loop through this and display each image:

<% for image in item.images.compact %>
  <%= image_tag('/my_images/' + image) %>
<% end %>

Notice the compact method, this will automatically remove any nil values in the array before looping over it.

Railscasts - Free Ruby on Rails Screencasts