Controller variables in layout

This is probably simple to most of the experts here, but what is the
best way to set variables to be used by a layout? Obviously, it
shouldn’t be in each routine in the controller as that would have to be
duplicated in every module - very error prone. Assuming you need to set
something based on the day of the month where would you put the code so
that the controller’s layout could use it.

Thanks in advance
—Michael

Probably in a before_filter. If you really want the data available
everywhere, put the before_filter in application.rb. If only for a
given controller, then implement it for that class only.

On Jun 7, 2007, at 5:12 PM, Michael S. wrote:

Thanks in advance
—Michael


Posted via http://www.ruby-forum.com/.

Steve R.
[email protected]
http://www.calicowebdev.com

On 6/7/07, Michael S. [email protected] wrote:

This is probably simple to most of the experts here, but what is the
best way to set variables to be used by a layout? Obviously, it
shouldn’t be in each routine in the controller as that would have to be
duplicated in every module - very error prone. Assuming you need to set
something based on the day of the month where would you put the code so
that the controller’s layout could use it.

Thanks in advance
—Michael

I think the answer partially depends on what data you are trying to
display. If the data is static, or related to something predictable,
like the date, I would create a helper function and create the data in
there. There might be cases where the data you want needs to come
from the database though, and for that I would create a before filter:

before_filter :do_stuff

def do_stuff
#stuff!
end

You should read up on before filters, there are more options that make
them conditional and such that can come in handy.

Read more here
http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html


F. Morgan Whitney
http://www.blizzo.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs