Forum: Ruby on Rails Defining a variable in the environment and using it in contr

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ivanvega (Guest)
on 2005-11-16 01:47
(Received via mailing list)
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.
Daniel.Berger (Guest)
on 2005-11-16 18:32
(Received via mailing list)
i.v.r. wrote:
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> 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
ivanvega (Guest)
on 2005-11-16 19:08
(Received via mailing list)
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
>
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:

<title><%= $pagemeta.title %></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.
ezra (Guest)
on 2005-11-16 20:18
(Received via mailing list)
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
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid
This topic is locked and can not be replied to.