[英]How can I pass variables from a controller to a layout helper in rails
I have a quite complex design template, which has different approaches to wrap it's context. 我有一个非常复杂的设计模板,它使用不同的方法包装上下文。
I have to use 我必须用
<body>
yield
</body>
sometimes 有时
<body>
<div class="foo">
yield
</div>
</body>
sometimes 有时
<body>
<div class="bar">
<div id="breadcrumbs">
some content
</div>
<div class="innnerwrap" id="zurb">
yield
</div>
</div>
</body>
and so on 等等
I wanna be able to control them from my controller. 我想能够从我的控制器中控制它们。 I thought I make a template_helper.rb
which decides what to show. 我以为我做了一个template_helper.rb
来决定要显示什么。
I see 2 approaches to this: 我看到两种解决方法:
1. Set your layout in your controller: 1.在控制器中设置布局:
class MyController < ApplicationController
layout 'admin', :only => [:show, :index]
# Controller methods
end
2. have 1 layout with multiple dynamic partials: 2.具有1个具有多个动态部分的布局:
layout.html.erb:
<body>
<% if params[:controller] == 'MyController' && params[:action] == 'MyAction' %>
<% render :partial => 'partial1' %>
<% elsif params[:controller] == 'MyController' && params[:action] == 'MyAction2' %>
<% render :partial => 'partial2' %>
<% elsif params[:controller] == 'MyController2' && params[:action] == 'MyAction' %>
<% render :partial => 'partial3' %>
<% end %>
</body>
_partial1.html.erb
<%= yield %>
_partial2.html.erb
<div class="foo">
<%= yield %>
</div>
_partial2.html.erb
<div class="bar">
<div id="breadcrumbs">
some content
</div>
<div class="innnerwrap" id="zurb">
<%= yield %>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.