我試圖最好地理解如何將軌道幫助程序與haml視圖一起使用。RoR:將代碼從視圖移至幫助程序
我最初包含這個邏輯
= fund_levels_last(i, @fund_level_count) ? (link_to "add new level", ad, {class: 'button orange sm'}) : (link_to "remove", accounts_ad_fund_level_path(ad, fl.object.id), {:class => 'button orange sm', :method => :delete, :remote => true, :confirm => t('q.are_you_sure')})
希望保持視圖乾淨的代碼可能一個觀點,我一直在嘗試這樣的邏輯移到幫手。
def fund_levels_last(i, flcount)
if i == flcount
true
else
false
end
end
def fund_levels_btn(i, flcount)
if self.fund_levels_last(i, flcount)
link_to "add new level", ad, {class: 'button orange sm'}
else
link_to "remove", accounts_ad_fund_level_path(ad, fl.object.id), {:class => 'button orange sm', :method => :delete, :remote => true, :confirm => t('q.are_you_sure')}
end
end
但是,在助手中,我無法訪問視圖中的變量和對象(ad,object,fl等)。我認爲我可以將所有這些傳遞給輔助方法,但不知何故,這似乎太複雜了,我有這種感覺,我在這裏走錯了路。我在視圖中的單行代碼似乎最終成爲助手中的15行代碼...
將此邏輯從視圖移動到助手的最簡單方法是什麼?
你可以看看https://github.com/drapergem/draper, – pjam