2017-04-21 72 views
1

我想創建一個幫助器,它遍歷用戶社區列表並創建與可用comunities數量一樣多的縮略圖。所以,我創建了這兩個輔助方法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 

所以我只是這個調用添加到我的觀點

<%= print_admined_group_thumbs %> 

它所有的工作幾乎是正確的,並創建所有縮略圖,就像我想,除了一兩件事。它還會在縮略圖之後打印出「group_hash」變量的全部內容。也許我今天太累了,但我似乎無法弄清楚爲什麼?如果有人幫我解決了這個問題,並解釋我在做什麼錯了,我會變得非常大膽。

回答

2

@some_hash.each {}在完成後自動返回散列。因此,您的函數print_admined_group_thumbs()將您的縮略圖添加到模板並返回散列。

的問題是在這裏:

<%= print_admined_group_thumbs %> 

=意味着「拿返回任何值,並將其添加到模板所以你不小心打印縮略圖模板後加入散列的模板。你可以很容易地通過移除=修復:

<% print_admined_group_thumbs %> 

這告訴Rails不增加它的返回值的模板運行功能

+0

這樣一個簡單的工作解決方案!你剛剛救了我的一天。非常感謝! – Konstantin

+0

很高興幫助c: – eiko