Need advice: Structure of Rails views for submenus?

前端 未结 2 624
执念已碎
执念已碎 2021-01-19 13:23

Imagine to have two RESTful controllers (UsersController, OffersController) and a PagesController (used for static content such as index, about and so on) in your applicatio

2条回答
  •  轮回少年
    2021-01-19 14:02

    One thing you could play with is yield and content_for, used with a few partials for the menus. For example you could put each section of the menu in a partial and then modify your layout to something like:

    <%= yield(:menu) %>
    

    You then can then specify in your views content_for and put whatever partials you want into the menu. If it's not specified it won't get rendered.

    <% content_for(:menu) do %>
       <%= render :partial => 'layouts/menu' %>
       <%= render :partial => 'layouts/search_menu' %>
        etc...
     <% end %>
    

    If you're using a lot of the same menus in most pages, specify a default in your layout if no yield(:menu) is found.

    <%= yield(:menu) || render :partial => 'layouts/menu_default' %>
    

    Saves you a lot of typing. :) I've found this to be a nice clean way of handling things.

提交回复
热议问题