带 Rails 的动态菜单(在一个视图中引用两个控制器的输出)

发布于 2024-12-15 21:21:20 字数 377 浏览 4 评论 0原文

我一直在尝试找到一种方法来显示应用程序从数据库生成的侧菜单。由于菜单需要可供用户编辑,我认为最好的做法是将其创建为脚手架,然后添加一个“显示菜单”操作,该操作将以树状方式呈现菜单,而其余部分脚手架将允许管理员用户修改菜单项。

现在,我遇到了一个小问题 - 我无法找到从 application.html.erb (我将其用作所有操作和控制器的包装器)“调用”显示菜单操作的方法。它有一个主要的“主体”,其中有一个“yield”行,这显然会让引用的任何控制器渲染其输出。在此之前,我想在该 html 的不同部分显示动态菜单。

这可能吗?人们将如何做到这一点?

因为无论显示哪个特定操作(视图),相同的菜单都应该是可见的,所以我想避免在应用程序的每个控制器的每个视图中放置相同的“菜单渲染”逻辑。

I've been trying to find a way to display a side-menu generated from a database by the application. Since the menu needed to be user-editable, I thought the best course of action would be to create it as a scaffold and just add a 'show menu' action that would render the menu in a tree-like fashion, while the rest of the scaffold would allow admin users to modify menu items.

Now, I'm presented with a small problem - I can not figure out a way to 'call' the show menu action from application.html.erb (which I'm using as a wrapper for all actions and controllers). It has a main 'body' where it has a 'yield' line, which will obviously let whichever controller is referenced to render it's output. Before that, I would have liked to show the dynamic menu in a different part of that html.

Is that possible, and how would one go about doing that?

Because the same menu is supposed to be visible regardless of which particular action (view) is being displayed, I wanted to avoid putting the same 'menu rendering' logic in every view of every controller of my application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-12-22 21:21:20

从您的术语来看,我不完全确定这就是您所需要的,但就这样。如果您想为不同类型的信息保留布局的不同部分,您可以这样做

<!-- In application.html.erb -->
<%= yield :menu %>
<!-- Main content goes here -->
<%= yield %>

然后在正在呈现的视图中,如果您想显示菜单,您可以执行以下操作。

<%= content_for :menu do %>
   <!-- Show menu -->
<% end %>

通过这种方式,您可以在您确实想要在其中而不是在其他视图中显示菜单的视图中显示菜单。

- -更新 - -
对于带有从数据库检索的菜单选项的动态菜单,请执行此操作

在您的 ApplicationController 中添加 before_filter

before_filter :fetch_menu


def fetch_menu
    @menu = #db query goes here
end

在 application.html.erb 中

<div id="menu">
   <%@menu.each do |menu|%>
      <!-- Do something with menu -->
   <% end %>
</div>

From your terminology I am not entirely sure that this is what you need but here goes. If you want to reserve different sections of a layout for different type of information you can do it like so

<!-- In application.html.erb -->
<%= yield :menu %>
<!-- Main content goes here -->
<%= yield %>

Then in the view that is being rendered you can do the following if you want to show the menu.

<%= content_for :menu do %>
   <!-- Show menu -->
<% end %>

This way you can show a menu in views that you do want to show a menu in and not in others.

---UPDATE---
For a dynamic menu with menu options retrieved from db do this

In your ApplicationController add an before_filter

before_filter :fetch_menu


def fetch_menu
    @menu = #db query goes here
end

In application.html.erb

<div id="menu">
   <%@menu.each do |menu|%>
      <!-- Do something with menu -->
   <% end %>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文