gpt4 book ai didi

ruby-on-rails - 为特定页面呈现特定标题

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:43 25 4
gpt4 key购买 nike

我是 RoR 的新手,使用布局/局部/渲染,所有这些好东西来制作应用程序。在我的应用程序开始时,我认为我会为整个应用程序使用相同的 header ,因此写道:

render 'layouts/header'

在我的 application.html.erb 文件中。现在,我意识到对于某些页面(即注册页面和博客)我想使用一个名为 simple_header 的简化 header 。我要写类似的东西吗

<%= if @simple_header
render 'layouts/simple_header'
else
render 'layouts/header'
%>

然后测试 @simple_header 是否为真或 Controller 中的某些内容,或者是否有更好的方法来执行此操作。谢谢。

最佳答案

在 Rails 中,您可以创建不同的布局并选择用于特定操作或 Controller 的布局。

观看此 railscast http://railscasts.com/episodes/7-all-about-layouts

这应该可以帮助您解决问题。

例如,

app/views/layouts 中创建一个新布局,比如 simple_header.html.erb 并在您的 Controller 中,告诉它使用该布局。

class BlogController < ApplicationController
layout :simple_header

def index
end

end

关于ruby-on-rails - 为特定页面呈现特定标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21213511/

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