2014-11-03 73 views
0

我有這個在我看來:如何使用&&如果得到此語句以正常工作?

<% if user_signed_in? && current_user.has_role? :admin or :editor %> 

這將返回該錯誤:

syntax error, unexpected tSYMBEG, expecting keyword_then or ';' or '\n' 

我也試過這樣:

<% if user_signed_in? and current_user.has_role? :admin or :editor %> 

雖然我沒有得到上面的錯誤,它根本不起作用......即non-signed-in-user可以訪問該if塊內的內容。

回答

1

我發現寫這更習慣的方法,它是利用[has_any_role?][1]

<% if user_signed_in? and current_user.has_any_role? :admin, :editor %> 
1

我不認爲has_role?可以採取多個參數。做正確的方法是:

<% if user_signed_in? && (current_user.has_role? :admin or current_user.has_role? :editor) %> 
+0

這確實有用,但不是很Ruby的慣用語。 – marcamillion 2014-11-03 14:35:24

0

你可以試試這個方法:

<% if user_signed_in? && [:admin, :editor].include?(current_user.role) %> 
+0

不...這不起作用。我得到以下錯誤:'語法錯誤,意外的tIDENTIFIER,期待keyword_then或';'或'\ n'' – marcamillion 2014-11-03 14:34:16

+0

我認爲失蹤()。 – 2014-11-03 14:35:49