gpt4 book ai didi

ruby-on-rails-3 - Rails 3 - 设置页面标题的理想方法

转载 作者:行者123 更新时间:2023-12-03 05:23:14 26 4
gpt4 key购买 nike

在 Rails 3 中设置页面标题的正确方法是什么。目前我正在执行以下操作:

app/views/layouts/application.html:

<head>
<title><%= render_title %></title>
<%= csrf_meta_tag %>

应用程序/helpers/application_helper.rb:

def render_title
return @title if defined?(@title)
"Generic Page Title"
end

应用程序/ Controller /some_controller.rb:

def show
@title = "some custom page title"
end

是否有另一种/更好的方法来执行上述操作?

最佳答案

你可以做一个简单的 helper :

def title(page_title)
content_for :title, page_title.to_s
end

在布局中使用它:

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

然后从您的模板中调用它:

<% title "Your custom title" %>

关于ruby-on-rails-3 - Rails 3 - 设置页面标题的理想方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3059704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com