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?
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 ;)
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With