Reputation: 1656
I got the following code in my view:
<ul class="thumbnails">
<% Photo.find_all_by_id(my_params, limit: 10).each do |p| %>
<li class="thumbnail">
<%= image_tag(p.url(:fb)) %>
</li>
<% end %>
</ul>
It renders an unordered list with thumbnails. And it works okay - I see the list with images:
<ul class="thumbnails">
<li class="thumbnail">
<img alt="bmw" src="/assets/.../bmw.jpg?1364218949">
</li>
</ul>
I would like to put it into helper, like this:
module PhotosHelper
def photos_thumbs_by_ids(photos_ids)
content_tag :ul, :class => "thumbnails" do
Photo.find_all_by_id(photos_ids, limit: 10).each do |p|
content_tag :li, :class => "thumbnail" do
image_tag(p.url(:fb))
end
end
end
end
end
But when I use <%= photos_thumbs_by_ids my_params %>
in my view, it renders only:
<ul class="thumbnails"></ul>
What am I doing wrong?
Upvotes: 0
Views: 128
Reputation: 534
The problem is the each :
Photo.find_all_by_id(photos_ids, limit: 10).each do |p|
...
end
It return an array, not the string. Try to collect it with 'map' and do a join
module PhotosHelper
def photos_thumbs_by_ids(photos_ids)
content_tag :ul, :class => "thumbnails" do
Photo.find_all_by_id(photos_ids, limit: 10).map { |p|
content_tag :li, :class => "thumbnail" do
image_tag(p.url(:fb))
end
}.join.html_safe
end
end
end
It's beacuse at least the rendering is done by the useful method capture. This method put in output buffer only if the result is a string, and don't try a to_s.
Upvotes: 1
Reputation: 2385
Try as follow;
<%= photos_thumbs_by_ids(photo_ids) %>
def photos_thumbs_by_ids(photos_ids)
content_tag :ul, :class => "thumbnails" do
Photo.find_all_by_id(photos_ids, limit: 10).each do |p|
content_tag :li, :class => "thumbnail" do
concat(image_tag(p.url(:fb)))
end
end
end
end
Upvotes: 1
Reputation: 1409
What i found after seeing your code is, you are not passing the photo_ids
parameter while calling the helper method.
<%= photos_thumbs_by_ids(photo_ids) %>
As there is no ids to find photos, so it is returning zero items, so no <li>
items will be created.
Upvotes: 1