Ruby on Rails助手打印出奇怪的东西 [英] Ruby on Rails helper prints out odd stuff

查看:88
本文介绍了Ruby on Rails助手打印出奇怪的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个遍历用户社区列表的助手,并创建与可用社区数量一样多的缩略图.因此,我创建了这两个辅助方法

I want to create a helper which iterates thru the list of user's communities and creates as many thumbnails as the number of comunities available. So I created these 2 helper methods

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 

所以我只需将此调用添加到我的视图

So I simply add this call to my view

<%= print_admined_group_thumbs %>

这一切几乎都能正常工作,并且可以创建所有缩略图,就像我想要的一样,除了一件事.它还在缩略图之后立即打印出"group_hash"变量的全部内容.也许我今天太累了,但我似乎不知道为什么?如果有人帮助我解决了这个问题,并解释了我在做什么错,我将不胜感激.

It all works almost correctly and creates all thumbnails just like I want, except for one thing. It also prints out the entire contents of "group_hash" variable right after the thumbnails. Maybe I'm just too exhausted today, but I can't seem to figure out why? I'd be greateful if somebody helped me solve this problem and explain what am I doing wrong with it?

推荐答案

@some_hash.each {}在完成后自动返回哈希值.因此,函数print_admined_group_thumbs()将缩略图添加到模板中并返回哈希值.

@some_hash.each {} automatically returns the hash after it completes. So your function print_admined_group_thumbs() adds your thumbnails to the template and returns the hash.

问题在这里:

<%= print_admined_group_thumbs %>

=的意思是取返回的任何值并将其添加到模板中.因此,在将缩略图打印到模板后,您不小心将哈希添加到模板中.您可以通过删除:

That = means "take whatever value is returned and add it to the template. So you're accidentally adding the hash to the template after printing the thumbnails to the template. You can easily fix it by removing the =:

<% print_admined_group_thumbs %>

这告诉Rails运行函数而不将其返回值添加到模板中.

This tells rails to run the function without adding its return value to the template.

这篇关于Ruby on Rails助手打印出奇怪的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆