Need advice: Structure of Rails views for submenus?

前端 未结 2 603
执念已碎
执念已碎 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.

    0 讨论(0)
  • 2021-01-19 14:03

    Typically, I would abstract out the menuing functionality so that I have a helper method for rendering the Admin menu. This way, it's possible to throw as much logic in the helper as you want without clouding up your view logic.

    So, your helper would look like (forgive the ruby/rails pseudo-code, it's been a month or two since I touched it):

    def render_admin_menu()
      if !current_path.contains('offer')
        render :partial => 'shared/admin_menu'
      end
    end
    
    0 讨论(0)
提交回复
热议问题