#30
May 11
Pretty Page Title
If you are like me, you avoid creating page titles because it is kind of a pain. But in this episode I will show you a clean way to add titles to your pages.
# application_helper.rb def title(page_title) content_for(:title) { page_title } end
<!-- layouts/application.rhtml --> <title>Shoppery - <%= yield(:title) || "The Place to Buy Stuff" %></title>



Used @page_title all the time :)
This screencast is useful for me. Can you create screencast about using HTML Tidy in Ruby on Rails? Thank you!
Great screencast! I took your idea one step further and had my title helper return either the set title or a default if one isn't already set when called with no arguments. That way I can call the helper to set the title, or call it w/ no arguments when I want to use the title.
Great idea Jake. I normally don't like giving methods a dual purpose, but in this case I think it works very well.
Hi! Could you make a cast for i18n and l10n? would be nice!
thanks! congratulations!
I was wondering why..
content_for(:title) { page_title }
works fine but
content_for :title { page_title }
doesn't - it gives a 500 error. Sorry.. I'm new to Rails/Ruby.
Another great tutorial Ryan!
Rob,
He says so during the screencast. It's due to the syntax reading past :title, I believe.
So use it with brackets for now.
Yep, Zach's correct. Ruby gets confused when you don't wrap the :title parameter in parentheses so it spits out a syntax error. I'm guessing it's because it thinks the {} brackets are trying to represent a hash not a block.
Same goes for later on in the video where I use parenthesis when calling yield(:title) while adding the || symbol. This is because Ruby doesn't know where to put the parenthesis, it could be here:
yield(:title || "foo")
or here:
yield(:title) || "foo"
It defaults to the first one (IIRC) so we need to clarify.
Whenever there's any doubt, wrap the parameters in parenthesis.
This is one of my favorite screencasts. I just redid all my views and they sure look purty!
Cheers!
great tutorial !!!!
thanks
another great screencast! After every casts I have to go through all my code and change/implement what I've just learned ;-)
I'd like to see more casts where you use helper methods, cause I think they are useful but I don't know when...
cheers
I just realized one caveat here. This particulat helper only takes a double-quoted string, so something like:
<%= title @article.title %>
Does not work because you will explicitly have to set it to a string
<%= title @article.title.to_s %>
This is not the case if you use the classic instance variable solution.
Woops! Ignore those = on the ERB tags.
@Pimpmaster, really? I've used non double quoted strings before. What kind of variable is @article.title? If it's not a string, that may be why. You may want to do the "to_s" conversion inside the title helper method.
Hi
thanks a lot for your work. Thats really what I was looking for, made a !little! donation today, hope that I can continue to donate :-D
Hopefully you have a lot of fun by doing that and you continue making such great "snippets".
greetinx
Thanks for donating Rafael! It really helps. I enjoy making the screencasts as well.
Hey Ryan, love the screencasts. Quick question... I noticed in this one that as you typed keyboard shortcuts they were displayed in the upper right corner of the screen. Just wondering how you did that. Is this a feature of Textmate?
I'm displaying the key commands with a little utility called KeyCastr.
http://stephendeken.net/software/keycastr/
Hi Ryan,
I would have a suggestion for a screencast.
How do you debug? With ruby-debug or breakpointer?
What is the better choice? There are topics, that breakpointer is deprecated, isn't it?
May this would be a topic for a Railscast
greetinx
Great cast!
Looks forward to another one
I used to use breakpointer a lot, but the recent Ruby releases broke it, so I'm currently getting by with just echoing variables to the log/view. I really need to learn ruby-debug, and once I do you can expect there to be an episode on it. Thanks for the suggestion.
It's the 13th! Do you know what that means?!?! Time for a new screencast! Whereisit whereisit whereisit....
Great screencast as always Ryan, I actually find myself enjoying yours more then peepcode, so be sure that I'm going to convert the money I would have spent on their screencasts into donation money for yourself, great work!
I do:
<title><%= h @page_title || "Set title in: views/#{params[:controller]}/#{params[:action]}.rhtml" %></title>
why dont you set the title in the controller so it can be used by several views?
I agree with foobar, seting title in view is kind of wrong, logic should go in controller
I see the title as view logic. The reason is because if a different controller action renders the same view, it doesn't have to set the title again.
For example, on the "new.rhtml" page you don't want to have to set the title in both the "new" action and the "create" action in the controller (in case the validation fails and the "new.rhtml" view is rendered again).
Now, if the title is complex obviously you don't want to put all that logic in the view. In that case it should be moved into a helper method.
I suppose this is personal preference, but I find placing it in the view is better.
Great site. This could probably have the refactoring tag added t it.
I've went a bit further: I have two helpers, access_denied() and action_denied() that put appropriate output; they now both have the title() call in them to set the title correctly, which is really nice. :)
I was setting @page_title in my controllers until I saw this screencast... thanks, Ryan!
I like the idea of using content_for, but I'm not sure I can perform some logic on it like I can with an instance variable. I'm using the title method in application_helper.rb, only setting an instance variable instead of content_for.
In my application.html.erb, I do:
<title>
<% if @page_title.nil? -%>
example.com | an example site
<% else -%>
<%= @page_title %> | example.com
<% end -%>
</title>
Can the same effect be achieved with content_for?
@Tim, yep, actually content_for is just setting an instance variable in the background. I believe it's called @content_for_title (or whatever you pass to the method). You can reference this instance variable directly if you need, or just stick with yield(:title) which basically does the same thing.
I like this method a lot - I just made one small change:
content_for(:title) { h(page_title) }
Adding the "h" method right to the helper ensures that no matter where I display it, it's safe. I often yield that title in a few other places, like in an h2 element somewhere on the page itself.
Really great tutorials, as always.
@Jeff Dean, Thanks for the tip about the "h" method. I was getting XHTML warnings about my titles before and that cleaned them right up.
@Ryan Bates, I think I have learned more about Ruby and Ruby on Rails from you than anyone on the planet. Thanks!