Class variables in templates/layouts

I’m having problems with class variables… I have a class:

class Content::ApplicantsController < ApplicationController
layout “mylayout”
@@tab = “mystring”
[… the rest is standard scaffold-created stuff …]

and a layout (mylayout.rhtml):

[…]

<%= @@tab.capitalize %> [...]

And I keep getting this error:

uninitialized class variable @@tab in
ActionView::Base::CompiledTemplates

I’ve searched the net and come up dry. Ruby docs say I’m supposed to
initialize class variables before I can use them but I’d assume that the
= "mystring" would count as initializing it…

i think it is deep in the language, that you actually have to initialize
it via the initialize(arg/args) definition, although i am not definately
sure…

what about if you do this inside the class -

def initialize(str)
@str = str
end

@@tab = Classinstancevar.new(“mystring”)

will this work?

Gabriel wrote:

[…]
I’ve searched the net and come up dry. Ruby docs say I’m supposed to
initialize class variables before I can use them but I’d assume that the
= "mystring" would count as initializing it…

I think rails only copies instance variables from the controller objects
to the view objects.


Jack C.
[email protected]

shai wrote:

what about if you do this inside the class -
def initialize(str)
@str = str
end
@@tab = Classinstancevar.new(“mystring”)
will this work?

I’m not sure… I don’t think I get the chance to initialize a
Controller manually…

Jack C. wrote:

I think rails only copies instance variables from the controller objects
to the view objects.

Hm… What’s the best way to get a variable to the view for all actions
if you can’t do it with a class variable?


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Hi –

On Thu, 6 Jul 2006, [email protected] wrote:

Controller manually…

Jack C. wrote:

I think rails only copies instance variables from the controller objects
to the view objects.

Hm… What’s the best way to get a variable to the view for all actions
if you can’t do it with a class variable?

Put it in an instance variable that you initialize in a before_filter
in application.rb.

David


“To fully realize the potential of Rails, it’s crucial that you take
the time to fully understand Ruby–and with “Ruby for Rails” David
has provided just what you need to help you achieve that goal.”
– DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS.
Complete foreword & sample chapters at Ruby for Rails!

Put it in an instance variable that you initialize in a before_filter
in application.rb.

Yeah I just came to that conclusion… I’d rather have a class variable.
:wink: