Hogan, Brian P.
2005-Nov-16 17:01 UTC
RE: Defining a variable in the environment and using itincontrollers and views
Well, environment is done once... So @pagemeta won''t work because
it''s a
scope issue. You can, however, create and set constants in the
environment.rb file, but you won''t be able to override them.
Here''s one way to achieve what you''re trying to do:
In /controllers/application.rb
# put this right after the class definition:
before_filter :setup
# set up any global variables that you want to use.
# Call this through a filter so it''s called on every request.
def setup
@pagemeta = {:title => ''Default page title'',
:description => ''Default page description'',
:keywords => ''Default page keywords''}
end
Then you should be able to use the variable in your pages.
Does that help?
-----Original Message-----
From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Daniel Berger
Sent: Wednesday, November 16, 2005 10:30 AM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails] Defining a variable in the environment and using
itincontrollers and views
i.v.r. wrote:> Hi,
>
> I wanted to define a global variable in environment.rb like this:
>
> @pagemeta = {:title => ''Default page title'',
> :description => ''Default page
description'',
> :keywords => ''Default page keywords''}
>
> And then, in a controller, override one or several of its values (like
> @pagemeta[:title] = ''some title''), and use it in the
views:
>
> <title><%= @pagemeta[:title] %></title>
>
> But Rails throws an error, apparently because it cannot access the
> @pagemeta variable inside the controller/view... How can I accomplish
> what I''m trying to do?
>
> Thanks in advance!
>
> Ivan V.
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
I, too, am confused by how the environment files work exactly. I''ve
looked
over pp 187-188, and 461-463, but I still don''t understand how an entry
you add
to one of the environment files becomes accessible to you in, say, a
controller, or even what an entry should look like.
Tips please?
Regards,
Dan
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails