Ask A Question


You’re not receiving notifications from this thread.

I18n access ".title" key with view context in layout

Nicolas Brousse asked in Rails

I try to create a small view helper to manage content for `<title>`.

module ApplicationHelper
  def page_title(options = {})
    app_name = Rails.application.class.to_s.split("::").first

    key = "#{controller_name}.#{action_name}.title"
    title = content_for(:page_title) || t(key, default: "")

    [app_name, title].join(" : ")

I use high_voltage gem. If I do `t(".title")` it attempt to call `en.pages.about.title` and in my helper it attempt to call ``.

I try to found a method to use instead `action_name` to get `about` instead `show` but I didn't found. 
I looked the rails code and found this method is used, but I can't access to the `@virtual_path`.

I think there is a proper way to do this, but I didn't found for now.  
Does somebody has any idea?

In fact `@virtual_path` is accessible inside `ApplicationHelper`. But it returns the path for the layout.

So finally what I would like, is to know the view name of the view called in the action controller.
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 78,890+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.