How do I add a simple menu to a Rails application? (How to use output: menu)

I want to add a menu to my First Ever Rails application. Nothing complicated. I developed that I should possibly reference it with application.html.erb, but after that I got stuck.

Here is what I have so far (it's not so much)

<%= render :partial => "menu" %> 

If I make a partial call to "menu" in application.html.erb, where do I put the menu file, and what do I call? Do I need to go to the view controller?

Can this be called partial from any folder that I enter?

Part II If I want to show other content in accordance with the presentation in which I participate - how to do it?

 <body> <p>[<%= yield :menu %>]</p> <%= yield %> </body> </html> 

I'm just learning Rails, so sorry for the stupid questions. In addition, I am interested not only in the solution, but also in the idea of ​​best practices.

+4
source share
1 answer

In basic terms, you're looking for a content_for helper. You put this into your views, which then populate named blocks in partial or layouts such as :menu . You can select, if you wish, partial parts for actually defining content for content_for areas.

In view:

 <% content_for :menu do %> <ul> <li> ... </li> <li> ... </li> </ul> <% end %> 

or how:

 <% content_for :menu do %> <%= render :partial => "some_menu_content" %> <% end %> 

In layout or partial:

 <div id="menu"> <%= yield :menu%> </div> 

See this screencast from the Railscasts series for more information. It is old but still applicable.

+5
source

Source: https://habr.com/ru/post/1315906/


All Articles