Sharing ruby code in view layout files

Hi,

I have a block of code that is used by the layout file (html.erb) of
every view. What is the best way to include this piece of code in all
layout files?

Can I actually include a Ruby file in an html.erb file, analogous to
how files are included in PHP?

Thanks,
Amrita

There is usually only one layout file. What are you trying to do? Views
can
share code by using helpers. You put them in application_helper.rb.

I have a block of code that is used by the layout file (html.erb) of
every view. What is the best way to include this piece of code in all
layout files?

Can I actually include a Ruby file in an html.erb file, analogous to
how files are included in PHP?

There is no need to do a “include”, helper methods in
application_helper.rb
are automatically made available to all the views.


http://www.rubyplus.org/
Free Ruby and Rails Screencasts

On Wed, 2008-07-16 at 14:36 -0700, amrita wrote:

Hi,

I have a block of code that is used by the layout file (html.erb) of
every view. What is the best way to include this piece of code in all
layout files?

Can I actually include a Ruby file in an html.erb file, analogous to
how files are included in PHP?


I don’t know that this is a ‘best’ way but I have app/view/shared and
then in the appropriate area in my layout, I have…

<%= render :partial => “shared/name_of_shared_code” %>

Craig

Thanks! I gathered something was wrong with the way my application was
built. So, I shifted to a single layout file and specified the layout
in the application controller.

But its always good to know how code can be shared.

Thanks, Craig and Bala.

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