Defining a variable in the environment and using it in contr

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.

i.v.r. wrote:


Rails mailing list
[email protected]
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

Daniel B. wrote:

(like @pagemeta[:title] = ‘some title’), and use it in the views:

Tips please?

Regards,

Dan


Rails mailing list
[email protected]
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.

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

509-577-7732
[email protected]