Konstantin Sidorov Konstantin Sidorov - 1 year ago 36
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

controller with three views:
. The layout for the controller goes like this:

<!DOCTYPE html>
<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' %>

<%= yield %>

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

<% '???'.each do |page| %>
<% end %>

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

Answer Source

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

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

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

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