i.v.r.
2005-Nov-15 23:46 UTC
Defining a variable in the environment and using it in controllers and views
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
2005-Nov-16 16:30 UTC
Re: Defining a variable in the environment and using it incontrollers 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/railsI, 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
i.v.r.
2005-Nov-16 17:07 UTC
Re: Defining a variable in the environment and using it incontrollers and views
Daniel Berger wrote:> 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 >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 Zygmuntowicz
2005-Nov-16 18:12 UTC
Re: Defining a variable in the environment and using it incontrollers and views
On Nov 16, 2005, at 8:30 AM, Daniel Berger wrote:> 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, > > DanGuys- 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 Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Maybe Matching Threads
- route error with controllers in a module, should just work
- Stories with many tags, tags with many stories, has_and_belongs_to_many howto?
- Best Environment for development
- Template "specialisation"
- Using :include "recursively" (including the children of the child)?