Rails: How to change the title of a page?
In your views do something like this:
<% content_for :title, "Title for specific page" %>
<!-- or -->
<h1><%= content_for(:title, "Title for specific page") %></h1>
The following goes in the layout file:
<head>
<title><%= yield(:title) %></title>
<!-- Additional header tags here -->
</head>
<body>
<!-- If all pages contain a headline tag, it's preferable to put that in the layout file too -->
<h1><%= yield(:title) %></h1>
</body>
It's also possible to encapsulate the content_for
and yield(:title)
statements in helper methods (as others have already suggested). However, in simple cases such as this one I like to put the necessary code directly into the specific views without custom helpers.
Rails 3 - Ideal way to set title of pages
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 ;)
Set a page title in rails
So, the answer already was in my question - the code I provided above,
<% unless current_page?(root_path) %>
<title>
<%= @board.title %> - MyChan
</title>
<% else %>
<title>MyChan</title>
<% end %>
works exactly as I need
Issue with dynamic page title/title in Ruby on Rails
content_for
isn't meant to render data 'in place', it returns nil
. Instead, you can define your title in the controller and reuse it in view, for example:
@title = 'Title for specific page'
and have in your view:
<% content_for :title, @title %>
<h1><%= @title %></h1>
rails page titles
I disagree with the other answers, I believe the title shouldn't be set per action, but rather within the view itself. Keep the view logic within the view and the controller logic within the controller.
Inside your application_helper.rb
add:
def title(page_title)
content_for(:title) { page_title }
end
Then to insert it into your <title>
:
<title><%= content_for?(:title) ? content_for(:title) : "Default Title" %></title>
So when you are in your views, you have access to all instance variables set from the controller and you can set it there. It keeps the clutter out of the controller as well.
<%- title "Reading #{@post.name}" %>
Set the title of Custom Pages in ActiveAdmin
In your provided link, you have the following options to modify the title of a page:
content 'title': Proc.new {
@page_title = 'Your title'
} do
...
end
or the other option, according the docs:
content title: "Your Title" do
...
end
According the docs, you have to see "Image or link in the title bar of a custom page" in the same link!
You using before_action
when register a controller
, but in the provided code, you are registering a page
.
Hope this helps!
Related Topics
How to Remove Rvm (Ruby Version Manager) from My System
How to Use the Conditional Operator (? :) in Ruby
Ssl Error When Installing Rubygems, Unable to Pull Data from 'Https://Rubygems.Org/
Installed Ruby 1.9.3 With Rvm But Command Line Doesn't Show Ruby -V
What Is Ruby'S Double-Colon '::'
Why Is Division in Ruby Returning an Integer Instead of Decimal Value
Ruby Class Instance Variable Vs. Class Variable
How to Write a Switch Statement in Ruby
What Does the (Unary) * Operator Do in This Ruby Code
Why Doesn't Ruby Support I++ or I-- (Increment/Decrement Operators)
What's the Difference Between Equal, Eql, ===, and ==
Rbenv Not Changing Ruby Version
Limitations in Running Ruby/Rails on Windows
How to Create an Average from a Ruby Array