Alexandre
Alexandre

Reputation: 13308

Rails - pass @@var to a view

Rails 3.2.3

I need to pass a class variable to a view. For some reason I'm unable to do this.

   class HomeController < ApplicationController
      @@user_id = '1343454'
      def index
       #.......................
      end
    def about
       #.......................
      end
    end

    /view/home/about.html.erb
    <% .... 
      @@user_id is not visible 
    ... %>

What's the easiest way to do it?

Upvotes: 0

Views: 86

Answers (1)

gabrielhilal
gabrielhilal

Reputation: 10769

Please do not use @@.

In your application controller you can define:

def current_user
  @current_user.id = '1343454'
end
helper_method :current_user

The helper method will make the current_user available in any controller or view in your application.

I believe, '1343454' is just an example. Usually we have something like:

@current_user ||= User.find(session[:user_id]) if session[:user_id]

Upvotes: 1

Related Questions