# Defining a variable in the environment and using it in contr

**URL:** <https://www.ruby-forum.com/t/defining-a-variable-in-the-environment-and-using-it-in-contr/48018>\
**Category:** Rails\
**Created:** [November 16, 2005, 12:47am UTC](https://www.ruby-forum.com/t/defining-a-variable-in-the-environment-and-using-it-in-contr/48018 "2005-11-16T00:47:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 16, 2005, 12:47am UTC](https://www.ruby-forum.com/t/defining-a-variable-in-the-environment-and-using-it-in-contr/48018/1 "2005-11-16T00:47:46Z")

</div>

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:

\<%= @pagemeta[: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.

---

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 16, 2005, 5:32pm UTC](https://www.ruby-forum.com/t/defining-a-variable-in-the-environment-and-using-it-in-contr/48018/2 "2005-11-16T17:32:47Z")

</div>

i.v.r. wrote:

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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

---

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 16, 2005, 6:08pm UTC](https://www.ruby-forum.com/t/defining-a-variable-in-the-environment-and-using-it-in-contr/48018/3 "2005-11-16T18:08:59Z")

</div>

Daniel B. wrote:

> > (like @pagemeta[:title] = ‘some title’), and use it in the views:
> 
> Tips please?
> 
> Regards,
> 
> Dan
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Hi,

I solved it using a module/class combination. In my environment.rb  
(actually on another file loaded by environment.rb, but it doesn’t  
really matter), I have this:

module MyModule  
class PageMeta  
attr\_writer :title, :description, :keywords  
attr\_reader :title, :description, :keywords  
def initialize(title, description, keywords)  
@title = title  
@description = description  
@keywords = keywords  
end  
end

$pagemeta = PageMeta.new(‘default title’, ‘default description’,  
‘some, keywords’)  
end

And then in my controllers I have (for example):

class WelcomeController \< ApplicationController  
def index  
$pagemeta.title = ‘overriding title’  
end  
end

And in a view I use:

\<%= $pagemeta.title %\>

It seems global variables (prefixed by the ‘$’ sign) aren’t really the  
Ruby way, but I couldn’t find an easier alternative…

Hope that helps.

Ivan V.

---

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 16, 2005, 7:18pm UTC](https://www.ruby-forum.com/t/defining-a-variable-in-the-environment-and-using-it-in-contr/48018/4 "2005-11-16T19:18:29Z")

</div>

On Nov 16, 2005, at 8:30 AM, Daniel B. wrote:

> > @pagemeta variable inside the controller/view… How can I  
> > 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

Guys-

```
You can't use a @pagemeta variable like that without an object that

```

contains it or something. Your best bet would be to use a constant  
for that instead. Then it will be automatically available everywhere  
in your app:

PAGE\_META = {:title =\> ‘Default page title’,  
:description =\> ‘Default page description’,  
:keywords =\> ‘Default page keywords’}

HTH-

-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid
