0
我有一个HTML模板,它应该根据控制器上的某些设置更改application.rb模板中的主体类。将控制器变量或参数传递给辅助模块
我知道如何做到这一点,当我想从视图中改变它。我这样做是这样的:
# in view
<% layout_class("full", boxed: false) %>
# helper method
module TemplateHelper
def layout_class(class_name="")
content_tag("body", :id => "fluidGridSystem", :class => class_name) do
yield
end
end
end
忘记上面的行!
我想使控制器
# index_controller.rb
class IndexController < ApplicationController
def index
@layout_class = "hello"
end
end
# app/helpers/template_helper.rb
module TemplateHelper
def body_wrapper
content_tag("body", :id => "fluidGridSystem", :class => @layout_class) do
if some_logiC# show <body> only
yield
else # add some more <div>'s
blog_wrapper do
yield
end
end
end
def blog_wrapper(inner="", outer="")
content_tag("div", :class => outer) do
content_tag("div", :class => inner) do
yield
end
end
end
end
end
# application.rb
<html>
<head>
</head>
<%= body_wrapper do %> # this part generates <body class="hello">
<%= flash_messages %>
<%= yield %>
<% end %> # </body>
</html>
但@layout_class
不传递给助手里这种情况发生。
- 我该怎么做?
- 或者是视图方法更好的解决?
- 原因是我想添加breadcrumbs和依赖于控制器逻辑的body类。
喜富不会公布的“类”属性,我已经更新了我的发布更好的解释,并添加了application.html.erb部分。请检查一下。我也删除了你指出的错字。我希望现在更清楚。非常感谢提前 – Jan
感谢您的更新 - 让我看看! –