gpt4 book ai didi

ruby-on-rails - rails : How to Render Conditional Based Upon 'pages/home.html.erb'

转载 作者:太空宇宙 更新时间:2023-11-03 16:20:51 26 4
gpt4 key购买 nike

application.html.erbtbody 部分,如果他在主页上,我想为 current_user 呈现两个侧边栏每隔一页只有一个侧边栏。

  <body>
<% if current_user.present? %>
<% if 'pages/home.html.erb' %> # Not Working As a Conditional
<div class="container">
<div class="col-md-3">
<%= render 'layouts/valuations' %>
</div>
<div class="col-md-6">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<div id="modal-holder"></div>
<%= yield %>
</div>
<div class="col-md-3">
<%= render 'layouts/sidebar' %>
</div>
</div>
<% else %>
<div class="container-fluid">
<div class="container">
<div class="col-md-9">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<div id="modal-holder"></div>
<%= yield %>
</div>
<div class="col-md-3">
<%= render 'layouts/sidebar' %>
</div>
</div>
</div>
<% end %>
<% else %>
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
</div>
<%= yield %>
<% end %>
</body>

最佳答案

首先,您正在寻找 current_page? :

<% if current_page?(controller: "pages", action: "home") %>
...
<% end %>

虽然这是出了名的不稳定(如果您更改 pages Controller 会发生什么?)。


你最好做的是 beartech 建议的,设置一个你可以在你的 layout 中 ping 的变量:

#app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
@home = true
end
end

#app/views/layouts/application.html.erb
<if @home %>
...
<% end %>

我会将其与 partials 的使用相匹配:

#app/views/layouts/application.html.erb
<%= render "shared/sidebar" if @home %>
<%= render "shared/flash" %>

这使您能够将代码拆分为更易于管理的 block 。我们在这里做:

enter image description here

关于ruby-on-rails - rails : How to Render Conditional Based Upon 'pages/home.html.erb' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33531750/

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