0

I have a rails 3 helper function. This function simply takes an array of ojects and return images based on the url in each object. For some reason, I cannot seem to print the images...I just get back the array.

for example:

def my_helper(items)
 items.each do |item|
   image_tag(item)
 end
end

this returns the array. I've tried assigning to a variable and outputting, but no luck. I've seen where people say just use item.join('<br/>') but I didn't get that to work.

Help appreicated

2 Answers 2

2

This should work:

def my_helper(items)
  items.map do |item|
    image_tag(item)
  end.join('<br/>')
end
Sign up to request clarification or add additional context in comments.

Comments

1

The each method returns the original list it iterates over (in your example, items). To get what you want, you can use map:

def my_helper(items)
  items.map do |item|
    image_tag(item)
  end
end

2 Comments

Hey thanks. I tried map, but it doesn't work. It returns an array of the text links, not the rendered images. So for example it returns: [<img src=image1.jpg/>, <img src=image2.jpg />], rather than rendering the images. Thanks for your help.
It's returning a string which wraps an array of strings...I don't need a string returned...that's my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.