2015-10-16 36 views
0

我在我的application_helper和兩行代碼(方法中的最後一個)中有一個方法,如果它們都處於活動狀態,它們將不起作用。如何以正確的方式對他們進行分組? (選擇代碼行取決於用戶打開的頁面)。在輔助方法中分組兩行代碼

def sortable(column, title = nil) 
    title ||= column.titleize 
    css_class = sort_column ? "current #{sort_direction}" : nil 
    direction = sort_column && sort_direction == "asc" ? "desc" : "asc" 
    link_to title, params.merge(sort: column, controller: 'analyze/consumptions', action: 'grid_report' , direction: direction), {class: css_class, remote: true, method: 'post'} 
    link_to title, params.merge(sort: column, controller: 'admin/users', action: 'records' , direction: direction), {class: css_class} 
end 
+0

你如何決定哪一個應該被激活? – SteveTurczyn

+0

這取決於用戶打開哪個頁面(在我的應用的2個不同頁面中使用排序表列) – Nadiya

+1

我不確定我是否理解。首先你提到「分組兩行代碼」,然後你說「選擇代碼行」。 「分組代碼行」到底意味着什麼? – Magnuss

回答

1

只有您的sortable輔助方法的返回值將被添加到緩衝區中。

您可以顯式連接希望包含的所有值(均爲link_to),並返回整個字符串。

def sortable(column, title = nil) 
    title ||= column.titleize 
    css_class = sort_column ? "current #{sort_direction}" : nil 
    direction = sort_column && sort_direction == "asc" ? "desc" : "asc" 

    "".html_safe.tap do |buffer| # Be sure you do not have unsafe content when using html_safe 
    buffer << link_to(title, params.merge(sort: column, controller: 'analyze/consumptions', action: 'grid_report' , direction: direction), {class: css_class, remote: true, method: 'post'}) 
    buffer << link_to(title, params.merge(sort: column, controller: 'admin/users', action: 'records' , direction: direction), {class: css_class}) 
    end # The entire buffer will be returned. 
end 

注:在我看來,像它會是更好的sortable分成兩種不同的方法,一個是你希望呈現的各個環節。

+0

謝謝,我把它分成兩種不同的方法 – Nadiya