开发者

Rails way to generate site navigation

开发者 https://www.devze.com 2023-04-09 18:31 出处:网络
Navigation for most websites takes the form of an html unordered list with anchors inside of the list elements.Typing out all of these item tags doesn\'t seem like the rails DRY way to create a list f

Navigation for most websites takes the form of an html unordered list with anchors inside of the list elements. Typing out all of these item tags doesn't seem like the rails DRY way to create a list for navigation.

I ask how you create a list for navigation the rails way and if you could help me develop the method I'm attempting described below...

what I've done is create a hash in my application_helper and then added a quick iteration code in my erb file to generate the list for me.

app helper:

               $navPages = { 
                          'top1' => "top1_path",
                          'top2' => "top2_path",
                          'top3' => "top3_path",

                          }

html.erb iteration code:

<ul>
           <% $navPages.each do |ntext,npath| %>
           <li><%= link_to ntext, self.send(npath.to_sym) %></li>
           <% end %>
</ul>

List output:

<ul>
    <li><a href="/">top1</a></li>
    <li><a href="/">top2</a></li>
    <li><a href="/"&g开发者_如何学Got;top3</a></li>
</ul>

That seems VERY rails to me.... I'm having a problem expanding on this for lists with "sub-items" or lists within lists.

I created the hash:

    $myHash = { 
           'top1' => { :location => "top1_path"},
           'top2' => { :location => "top2_path", :members => { 
                          "sub1-1" => { :location => "sub1_path"},
                          "sub1-2" => { :location => "sub2_path"},
                          "sub1-3" => { :location => "sub3_path"},

                          }
           },
           'top3' => { :location => "top3_path", :members => { 
                          "sub2-1" => { :location => "sub1_path"},
                          "sub2-2" => { :location => "sub2_path"},
                          "sub2-3" => { :location => "sub3_path"},

                          }
           }


               }

I have tried many way to convert that hash into an unorded list with anchors but I haven't found a clean solution that works perfectly. Any thoughts on how to do this? The reason I like hashes for this task is that I can capture the association of items as well as other useful information such as the link location I've stored in the :location symbol.

I'm thinking that the hash could/should be traded in for something with less typing... like

top1 loc
  member1 loc
  member2 loc
top2 loc

not sure where to stat on that though :(

so... to Generate an HTML list with this information just doesn't seem very railsy to me... what is everyone on rails doing?

Thanks!

thanks!


My recommendation is to look into using the Simple Navigation Gem. This has a nice DSL for defining navigation, and you have complete control over highlighting, submenu display and classes and ids used.

Here is an example of the DSL


Common practice is to have couple erb files, something like shared/_nav.html.erb, shared/_subnav.html.erb for the reasons:

  • premature abstraction is not good
  • navigation changes not often

What means Rails way, is:

  • DRY
  • Conventions over configuration
  • Patterns set, like MVC, Delegate, Proxy, Observer, STI and so
  • Fat models, skinny controllers
  • Human readable coding standards
  • Unobtrusive JS

So Rails do not cover specific client-side representation things, then you can do it to make code clear and simple.

My own opinion is better to build navigation with JS, because it's easier to do mobile markup based on it

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号