避免重复的“content_for”在视图中
我的布局中放置了一个子菜单,该子菜单因控制器而异,但在每个控制器方法视图之间则不同。我目前正在做的事情如下:
<% content_for( :submenu ) do %>
<%= render :partial => 'submenus/correct_submenu' %>
<% end %>
在方法的每个视图中
我的应用程序布局中都有这个
<%= yield :submenu %>
但是,这感觉有点重复,为每个视图执行此操作。每个控制器有什么方法可以做到这一点吗?
I have a submenu placed in my layout wich differs from controller to controller, but not between each controllers method views. What I am currently doing is the following:
<% content_for( :submenu ) do %>
<%= render :partial => 'submenus/correct_submenu' %>
<% end %>
In every view for a method
My applications layout then has this in it
<%= yield :submenu %>
However, this feels kind of repetitive, doing it for each view. Is there some way to do this per controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的建议是为此制定一个约定,因此如果您有一个
ProductsController
,那么子菜单将为submenus/products_menu
。这样您就可以编写一个如下所示的帮助程序:然后您可以通过执行以下操作来调用它:
然后您可以将其设为子菜单的默认 content_,并且仅在需要不同时才指定内容。
我希望这有帮助!
My suggest is to have a convention for this, so if you have a
ProductsController
then the submenu would besubmenus/products_menu
. This way you can write a helper that looks like:You can then call this by doing:
You could then make this the default content_for the submenus and only specify the content if it needs to be different.
I hope this helps!
使用 嵌套布局 将特定控制器的布局嵌套在应用程序布局下,方法是像这样创建一个文件:
使用这种方法,您不必修改一堆视图文件。
Use nested layouts to nest a specific controller's layout under the application layout, by creating a file like so:
With this method, you don't have to modify a bunch of view files.