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, ie 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.
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>
You can render something from the database in the controller and use it in the views. Like in the controller, I can write @ones = Something.all
in a method called one
and if I have a show method in the controller who shows one record from the database depending on the argument it takes, then I can add a link as below and it will take me to something_show_path/:something_id
.
<% @ones.each do |one|%>
<%= link_to "#{one}", something_show_path(@one) %>
<% end %>
But in this case, there is no record of the static pages you have in the database. So, if you want a syntax like that in the views, add them in an array in the controller like
def whatever
```rest of the code````
@pages = ["home", "about", "help"]
end
And in the view, you can write
<ul>
<% @pages.each do |p| %>
<li><%= link_to("#{p}", :controller => 'static_page', :action => "#{p}") %></li>
<% end %>
</ul>
Edit for the question in the comment
class StaticPagesController < ApplicationController
before_filter :load_pages, :only => [:about, :home, :help]
``` All your controller codes ```
private
def load_pages
@pages = ["load", "about", "help"]
end
end
So that all the methods in the :only
list will have @pages loaded before rendering the page. You don't have to load it in each method. you can declare the method in the ApplicationController
by writing a private before it like I did it here and just call it in any controller with the before_filter
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.