简体   繁体   English

为“设计”页面设置浏览器和页面标题吗?

[英]Set a browser and page title for Devise pages?

I have methods that set a title for the browser and for the page itself. 我有为浏览器和页面本身设置标题的方法。 On my Devise pages I would like to set these two methods but am not sure how to. 在我的Devise页面上,我想设置这两种方法,但是不确定如何设置。

My Code: 我的代码:

helpers/application_helper helpers / application_helper

def title # for entire application
    base_title = "Testing"
  if @title.nil?
    base_title
  else
    "#{base_title} - #{@title}"
  end
end

def page_title # for page header partial
   "#{@page_title}"
end

views/layouts/application 视图/布局/应用程序

<!DOCTYPE html>
<html>
   <head>
      <title><%= title %></title>
      <%= csrf_meta_tag %>
      <%= favicon_link_tag %>
      <%= stylesheet_link_tag "application" %>
      <%= javascript_include_tag "application" %>
   </head>
   <body>
      <div id="container">
         <%= render "shared/page_header" %>
         <%= render "shared/flash_message" %>
         <%= yield %>
      </div>
   </body>
</html>

views/shared/_page_header 视图/共享/ _page_header

<% unless @page_title.blank? %>
  <div id="page-header">
    <span><%= @page_title %></span>
  </div>
<% end %>

Now I have a RegistrationsController to override the functionality whenever I need to but as it inherits to the DeviseController, I don't think it can get to the Application Helper? 现在,我有一个RegistrationsController可以在需要时覆盖该功能,但是由于它继承了DeviseController,所以我认为它无法访问Application Helper吗? I also tried to put this same code in the Registration Helper but that didn't work either. 我还尝试将相同的代码放入“注册帮助器”中,但这也不起作用。 What should I do? 我该怎么办?

Maybe you can use; 也许你可以使用;

application_helper.rb application_helper.rb

module ApplicationHelper
  def title(page_title)
    content_for(:title) { page_title }
  end
end

application.html.erb application.html.erb

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

view 视图

<%= title "Your page title here" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM