t56k
t56k

Reputation: 6981

Rails: check and redirect unless boolean

Sounds simple, right? I've got this method in the ApplicationController:

def authenticate_admin
  redirect_to :root_path unless current_user.admin?
end

And it's called in various other controllers like so:

before_filter authenticate_admin

But I get an error that any instance variables within methods that that filter is applied to are returned nil:

<% @expressions.each do |expression| %>

Any thoughts? It seems bizarre to me.

Upvotes: 0

Views: 43

Answers (1)

Mohamad
Mohamad

Reputation: 35349

Are you passing a symbol to the filter? You need to pass it a symbol. By the looks of it you are passing it a variable name, which does not exist.

before_filter :authenticate_admin # correct usage

Your example shows this:

before_filter authenticate_admin # incorrect

Notice the missing colon.

Upvotes: 1

Related Questions