2013-08-05 81 views
0

我在軌道上瀏覽了一些關於塊助手的內容,但是我比之前開始的時候更加困惑。 我的目標是創建這樣欄杆助手在欄杆中

<% needs_clearance 1 do %> 
    You'll see this block if your clearance is level 1, 4 or 5 
<% end %> 

一個幫手應該產生

<% if current_user.clearance.id == 1 or current_user.clearance_id == 4 or current_user.clearance.id == 5 %> 
    You'll see this block if your clearance is level 1, 4 or 5 
<% end %> 

4和5分別爲管理和管理角色。
如何創建此塊助手?

+0

具體問題是什麼?你應該能夠做到你的邏輯,並抓住你的塊的結果? –

+0

我真的不知道如何建立一個塊助手,我找不到一個我可以用作指導的例子。我希望有人可以發佈一個幫助我的人,讓我可以用它作爲例子。 –

回答

1

大致爲:

def needs_clearance(level, &block) 
    if current_user.clearance >= level 
    capture(&block) 
    end 
end 

塊傭工一般如下所示,例如,在一個div包裹的東西:

def box(&block) 
    "<div class='box'>" + capture(&block) + "</div>" 
end 

來源:http://timelessrepo.com/block-helpers-in-rails3

我會考慮讀取,因爲它討論了塊幫助者的一個有趣的結果。

+0

現在有道理。謝謝! –