Dan Eden
Dan Eden

Reputation: 1634

Access OmniAuth helper in Sinatra view

I'm new to this, so forgive me if I'm being stupid!

I'm using OmniAuth for Sinatra to help authenticate users. In my layout.erb, I'd like to access the current_user helper to check if the user is logged in. The helper goes like this:

def current_user
    @current_user ||= User.get(session[:user_id]) if session[:user_id]
end

But I'm not sure how to access it in my layout view. I thought this would do the trick:

<% if current_user %> Do stuff here <% end %>

But no luck. Any help would be appreciated! Like I said, I'm new to Ruby, and I'm not a strong developer.

Upvotes: 2

Views: 247

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107718

You need to define that method inside helpers block, like this:

helpers do
  def current_user
    @current_user ||= User.get(session[:user_id]) if session[:user_id]
  end
end

Then it will be available to the view.

Upvotes: 1

Related Questions