0

I have this in my view:

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

This returns this error:

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

I also tried this:

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

And while I don't get the above error, it doesn't work at all...i.e. a non-signed-in-user can access the content within that if block.

Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
marcamillion
  • 32,933
  • 55
  • 189
  • 380

3 Answers3

1

I don't think has_role? can take more than one parameter. The correct way to do it is:

<% if user_signed_in? && (current_user.has_role? :admin or current_user.has_role? :editor) %>
Alireza
  • 2,641
  • 17
  • 18
1

I found the more idiomatic way to write this, which is using [has_any_role?][1]:

    <% if user_signed_in? and current_user.has_any_role? :admin, :editor %>
marcamillion
  • 32,933
  • 55
  • 189
  • 380
0

You can, try this way:

<% if user_signed_in? && [:admin, :editor].include?(current_user.role) %>
Alejandro Babio
  • 5,189
  • 17
  • 28
  • Nope...this doesn't work. I get the following error: `syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'` – marcamillion Nov 03 '14 at 14:34