Vishwa Rao wrote:
>
> OK. I got the welcome page working to look at application.html.erb file
> by creating a controller 'home' and updating the routes.rb with a line
> map.root :controller => "home"
> Now, if I customize home/index.html.erb then I can display whatever I
> need to - gets called at <%= yield %>, I hope - Is this the right
> approach?
>
> I still trying to understand yield(:head) and yield(:title) - can you
> pl. tell me?
The idea behind yield flows with a method called content_for().
In your application_helper.rb file in the helpers folder place the
following:
def title(page_title)
content_for(:title) { page_title }
end
Now then, let's say you have a title in your layout that shows this:
<title><%= "My Home Page" || yield(:title) %></title>
On every single page that a person opens up on your site that uses the
layout wrapper, it would show the same exact title of "My Home Page".
You wouldn't want the same title for every page would you? No, probably
not.
So, with the helper file I just showed you how to make you could do the
following:
Let's say you have a new page called index.html.erb for a new controller
called administration. So inside the index.html.erb you could place at
the top of that file the following line:
<% title("Administration Page" %>
Now then, whenever that pages loads it will automatically see that you
are calling a content_for method for title("some label") and the helper
file will place the new title there instead of the old one. So, on the
admin page, you will see Administration Page and not My Home Page.
This allows you to decide what type of content to use on different
pages.
Let's add another helper for the head.
def head(css_file)
content_for(:head) { stylesheet_link_tag "#{css_file}" }
end
Again, using this example, you might want to specifically overwrite your
css with some specific css file that should only be used on one or two
pages on your site. On these pages within your views, you could do:
<% head('my_custom_css') %>
Which will be yielded where?.. That's right, in the head section where
you wanted it to go. It's the same thing as placing a
stylesheet_link_tag 'my_custom_css' in the head of that page only.
Yielding content_for elements in specific places allows you to define
specific content to appear in certain places of the layout, but only on
certain pages.
I hope that helps you understand that a little bit. I would try it out
on several pages and see how it works.
Please keep in mind that I'm not advocating loading multiple css files.
But, there are times you might require multiple css files on a specific
page. So, I'm just supplying you with a basic understanding of how to
use helpers to define content_for methods on specific pages which are
included within your layout only for those specific pages.
I hope I made things clear enough for you.
Take care.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.