2014-11-03 77 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