Ruby on Rails助手打印出奇怪的东西

我想创建一个帮助程序,它通过用户社区列表进行迭代,并创建与可用社区数量一样多的缩略图。 所以我创建了这两个辅助方法

def print_admined_group_thumbs @user_groups_hash[:admined_groups].each { |group_hash| name = group_hash[:name] group_id = group_hash[:group_id] photo = group_hash[:photo] members_count = group_hash[:members_count].to_i concat(get_group_thumbnail_html(group_id, name, members_count, photo)) } end # creates a small preview for the selected comunity group def get_group_thumbnail_html(group_id, name, members_count, photo) content_tag(:div, :class => "col-md-55") do concat(content_tag( :div, :class => 'thumbnail') do concat(content_tag(:div, :class => 'image view view-first') do concat(image_tag(photo, :class => "group_thumb_image")) concat(content_tag(:div, :class => "mask") do concat(content_tag :p, "Your text") concat(content_tag(:div, :class => "tools tools-bottom") do end) end) concat(content_tag(:div, :class => "caption") do concat(content_tag(:p, name)) end) end) end) end end #end get_group_thumbnail_html 

所以我只是将这个调用添加到我的视图中

  

这一切几乎都正常工作,并创建所有缩略图,就像我想要的那样,除了一件事。 它还会在缩略图后面打印出“group_hash”变量的全部内容。 也许我今天太累了,但我似乎无法弄明白为什么? 如果有人帮助我解决这个问题并解释我做错了什么,我会很感激吗?

@some_hash.each {}在完成后自动返回哈希。 所以你的函数print_admined_group_thumbs()将你的缩略图添加到模板并返回哈希。

问题出在这里:

 <%= print_admined_group_thumbs %> 

=意味着“获取返回的任何值并将其添加到模板中。因此,在将缩略图打印到模板后,您不小心将哈希添加到模板中。您可以通过删除=来轻松修复它:

 <% print_admined_group_thumbs %> 

这告诉rails运行该函数而不将其返回值添加到模板。