Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Ideal way to set title of pages

Whats the proper way to set the page title in rails 3. Currently I'm doing the following:

app/views/layouts/application.html:

<head>   <title><%= render_title %></title>   <%= csrf_meta_tag %> 

app/helpers/application_helper.rb:

def render_title   return @title if defined?(@title)   "Generic Page Title" end 

app/controllers/some_controller.rb:

def show   @title = "some custom page title" end 

Is there another/better way of doing the above?

like image 381
akfalcon Avatar asked Jun 17 '10 07:06

akfalcon


2 Answers

you could a simple helper:

def title(page_title)   content_for :title, page_title.to_s end 

use it in your layout:

<title><%= yield(:title) %></title> 

then call it from your templates:

<% title "Your custom title" %> 

hope this helps ;)

like image 152
Andrea Pavoni Avatar answered Oct 10 '22 12:10

Andrea Pavoni


There's no need to create any extra function/helper. You should have a look to the documentation.

In the application layout

<% if content_for?(:title) %>   <%= content_for(:title) %> <% else %>   <title>Default title</title> <% end %> 

In the specific layout

<% content_for :title do %>   <title>Custom title</title> <% end %> 
like image 24
griable Avatar answered Oct 10 '22 11:10

griable