The starter site that I created and uploaded is just a default root page with no additional pages. It’s not much of a website yet. ^_^ The next class in One Month Rails had me creating pages and learning how to set one as root. It also created a navigation bar that allowed me to go from one page to another. It was a little difficult to understand at first because I’m used to just clicking “new post” on some various GUI to get new pages to appear. But it made sense after a little bit of noodling. 🙂
How to set up the first page:
$ rails generate controller Pages home # Create a Pages controller with a home action $ rm public/index.html # public/index.html will automatically override any root route set
It was still important to set up that new Home page that I created above to make it the root page when my site is first loaded. This is done in the /config/routes.rb file by adding the following:
root :to => 'pages#home'
I also added another page there by doing:
get 'about' => 'pages#about'
However, that won’t really work because there isn’t an about page actually created yet. Creating the page was as easy as making a new file, about.html.erb in app/views/pages.
Both pages have to also be in the controller, otherwise they won’t be controlled, I guess.
class PagesController < ApplicationController def home end def about end end
I also learned about the applications.html.erb file which apparently is called by every page on the site and comes along with every Rails project. The only thing that actually changes what content is shown on each page is the <%= yield %> section. Originally, I edited this file to put the HTML content for my header and footer, but then the course had me create two separate files for these under app/views/layouts called _header.html.erb and _footer.html.erb. Then, instead of actually typing out all their contents in the applications.html.erb file, I used ‘render’ to pull these things in above (for the header) and below (for the footer) the <%= yield %> section. Here’s the code for how to render contents from another file in the project:
<%= render 'layouts/footer' %> <%= render 'layouts/header' %>
Linking to another page:
Here is the syntax:
<%= link_to "Home", root_path %> <%= link_to "About", about_path %>
So that was it for my page! I also ended up with pushing it up to Git and Heroku so I could see it live in the world.