2012-12-28 57 views
0

我在我的home.html.erb文件中有以下代碼;根據用戶角色在rails中顯示和隱藏

<!-- if seeker is logged in show button "Grab it" --> 
<% if user_signed_in? %> 
<div class="grabit alignright"> 
<small>want to work on this?</small> 
<!-- if seeker is not logged in, show him the output text of "Sign in to work on this job" --> 
<% else %> 
<small>are you a guru? want to work on this? Sign up.</small> 
<% end %> 
</div> 

現在,您可以看到我試圖讓Seeker成爲用戶角色。取決於具有該角色類型的用戶類型是否已登錄,將顯示不同的內容。

我已經安裝了Devise + CanCan。現在對此非常新穎,我已經全面瞭解如何做到這一點。我將擁有具有尋求角色的普通用戶和「搜索者」角色的「用戶」。使用上面的代碼只顯示用戶登錄時,而不是尋求者。

是否像seekers_signed_in那麼簡單?我應該使用?與user_signed_in相比?我爲用戶和搜索者以及管理員生成了Devise視圖。管理員可以爲用戶和搜索者刪除,更新等。用戶發佈項目和搜索者抓住他們。

任何人都可以幫忙嗎?

回答

0

您不必創建用戶&搜索者(兩個設計模型),而只能創建一個普通模型並將其稱爲User,然後根據需要添加任意數量的角色。

我建議使用此gem,方便角色配置,

然後在你的home.html.erb你只需做到以下幾點:

<!-- if seeker is logged in show button "Grab it" --> 
<% if user_signed_in? && current_user.is_seeker? %> 
<div class="grabit alignright"> 
<small>want to work on this?</small> 
<!-- if seeker is not logged in, show him the output text of "Sign in to work on this job" --> 
<% else if !user_signed_in?%> 
<small>are you a guru? want to work on this? Sign up.</small> 
<% else%> 
<small>You are not a seeker, you need to be a seeker!</small> 
<% end %> 
</div> 

康康舞在控制器級別使用,因此上述方法是簡單且直接爲你的情況。

我希望這會有所幫助。

+0

嗨,這樣做是否也適用於搜索者和用戶有不同觀點的方法?而不僅僅是一個簡單的home.html.erb頁面? -謝謝! – user1924165

+0

因爲你可以訪問user_signed_in嗎? && current_user從任何視圖中,您將能夠在任何視圖中檢查current_user是否爲尋求者或不是..請記住,它的一個用戶模型已用於所有應用程序,您是否檢查了簡單角色gem? – simo

相關問題