2015-09-30 52 views
0

我有以下簡單的助手,其工作原理:Rails的定製輔助生成HTML

module MyHelper 
    def widget 
    link_to new_flag_path do 
     content_tag(:i, '', class: "fa fa-flag") 
    end 
    end 
end 

我想輸出的第二鏈路,例如:

module MyHelper 
    def widget 
     link_to new_flag_path do 
     content_tag(:i, '', class: "fa fa-flag") 
     end 
     link_to new_comment_path do 
     content_tag(:i, '', class: "fa fa-comment") 
     end 
    end 
    end 

的pugautomatic本文中列出的解決方案使用「concat」連接單個塊助手內的多個助手。 http://thepugautomatic.com/2013/06/helpers/這適用於標準的link_to助手如:

module MyHelper 
    def widget 
    concat link_to("Hello", hello_path) 
    concat " " 
    concat link_to("Bye", goodbye_path) 
    end 
end 

當A HREF使用glyphon你需要使用的link_to塊幫手,如:

link_to new_comment_path do 
    content_tag(:i, '', class: "fa fa-comment") 
end 

的毗連不允許我Concat的多link_to幫助者如:

module MyHelper 
    def widget 
    concat link_to new_flag_path do 
     content_tag(:i, '', class: "fa fa-flag") 
    end 
    concat link_to new_comment_path do 
     content_tag(:i, '', class: "fa fa-comment") 
    end 
    end 
end 

這種情況下的解決方案是什麼?

+0

[鋼軌可能重複4格式的自定義輔助方法,不考慮獲取輸出 - 完全](http://stackoverflow.com/questions/31682206/rails-4-custom-helper-method-with-form-doesnt-get-output-in-view-fully) – Pavan

+0

我熟悉解決方案鏈接的文章,但它現在在這種情況下工作。 Concat只能在link_to內使用,因此不允許鏈接多個link_to助手。 – Dercni

+0

fontawesome-sass給你'icon(:flag)'。只是說在 –

回答

0

我認爲你必須把每一個的link_to到單獨的方法:

module MyHelper 
    def link_to_flag_page 
    link_to new_flag_path do 
     content_tag(:i, '', class: "fa fa-flag") 
    end 
    end 

    def link_to_new_comment 
    link_to new_comment_path do 
     content_tag(:i, '', class: "fa fa-comment") 
    end 
    end 
end 

,並呼籲他們一個接一個

+0

上面的例子是一個幫助器的簡化版本,有很多邏輯需要從一個幫助器方法輸出兩個鏈接。 – Dercni

+0

但是你展示的不是輔助方法,它只是一個模塊。你能把這個邏輯封裝到一個方法中,然後從視圖中調用它嗎? – Matheus208

+0

更新後的代碼,是一個疏忽。 – Dercni