Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active December 22, 2015 23:58
Show Gist options
  • Save henrik/6550039 to your computer and use it in GitHub Desktop.
Save henrik/6550039 to your computer and use it in GitHub Desktop.
Magic title helper for Rails: for FoosController#show, it uses `t(:"foos.show.title")` unless you do `<% self.title = "Something" %>` in the view.
html
head
title= title("My site")
module TitleHelper
def title=(title)
@page_title = title
end
def title(base_title = nil)
[page_title, base_title].compact.join(" – ")
end
private
def page_title
@page_title || default_page_title
end
def default_page_title
# Reinventing t(:".foo") since this is called from a layout.
key = (
controller.controller_path.split("/") +
[controller.action_name, "title"]
).join(".")
t(key, default: "").presence
end
end
@henrik
Copy link
Author

henrik commented Sep 13, 2013

I should spec and gemify this. I think I already have a gem from a previous implementation of something similar…

@henrik
Copy link
Author

henrik commented Sep 13, 2013

FIXME: if e.g. #create renders "new", this breaks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment