Sharing code in views

I am new to Rails and am wondering how to share code across views. I
have two views and want to share website logo & header. Any thoughts
would be very helpful.

Thanks.

You need to use layouts. Create one called application.rhtml which is
where you keep the site-wide html. For an excellent introduction to
layouts, see this screencast:
http://railscasts.com/episodes/7

Hope that helps,

DAZ

Use a partial. It’s just a file that starts with an underscore in your
views folder. And then call it via render :partial => :logo

All partials & views can access instance variables set in your
controller. Partials can also be passed local variables through a
:locals hash.

http://railsbrain.com/api/rails-1.2.3/doc/index.html?a=M000463&name=render

Vish

These are the most common and simple ways of sharing code:

You have two methods, depending on what you want to achieve. A
layout under app/views/layout allows you to declare a page that
serves as a shell for your content and then you can add a
content_for_layout reference where you want your content to appear.
You can even have multiple layouts. Another way to share code is to
create partials, such as a common header or footer, that your views
can include.

Paul

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