Konstantin Sidorov Konstantin Sidorov - 4 months ago 10
Ruby Question

Iterating over views in Ruby on Rails

I'm a Ruby on Rails noob. I work through Michael Hartl's tutorial on Ruby on Rails. So far, I'm on chapter 3 about static pages. So far, I have

StaticPage
controller with three views:
home
,
help
and
about
. The layout for the controller goes like this:

<!DOCTYPE html>
<html>
<head>
<title><%= yield(:title) %> | Sample App</title>
<%= csrf_meta_tags %>

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

<body>
<%= yield %>
</body>
</html>


What I want to do now is to add menubar, i.e. list of links to all static pages. So far, I have something like this at
<body>
tag:

<ul>
<% '???'.each do |page| %>
<li>Page!</li>
<% end %>
</ul>


I can't figure out what to put instead of
'???'
- I need an iterator of all views of a controller. Thanks in advance.

Answer

What you are asking for requires more than adding static pages. You will need a database backed model to store dynamically created pages and a controller to work with the model actions which gives you the ability to generate collections.

With collections you can then have something like

<ul>
  <% '???'.each do |page| %>
    <li><%= page %></li>
  <% end %>
</ul>

Since you are working with that tutorial, what you need to render your menubar is this

<ul>
  <li><%= link_to "Home", home_path %></li>
  <li><%= link_to "Help", help_path %></li>
  <li><%= link_to "About", about_path %></li>
</ul>
Comments