Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class variables in rails views?

Is it possible to refer to ruby class variables in a view?

like image 580
Samantha Bennett Avatar asked Oct 15 '09 21:10

Samantha Bennett


1 Answers

The more common approach is to wrap the class variable in a helper method:

# in /app/controllers/foo_controller.rb:
class FooController < ApplicationController
  @@bar = 'baz'

  def my_action
  end

  helper_method :bar

  def bar
    @@bar
  end
end

# in /app/views/foo/my_action.html.erb:
It might be a class variable, or it might not, but bar is "<%= bar -%>."
like image 83
James A. Rosen Avatar answered Sep 21 '22 19:09

James A. Rosen