Monday, April 21, 2014

Creating static pages on Rails

Recently, I had to create three new pages for Demigod's website. These pages had a common characteristic, because all of them were based on static content (i.e., no data or information collected from a database, user input, etc.). So, I needed three pages to write a description of one each of the categories that we use to classify our challenges (Nutritional, Physical, and Intellectual).

I started creating three separate actions in the home controller (/app/controllers/home_controller.rb):

class HomeController < ApplicationController
  (...)

  def nutritional
  end

  def physical
  end

  def intellectual
  end
end

Then, I added the corresponding routes for each one of the actions (/config/routes.rb):

get "/nutritional", to: "home#nutritional", as: "nutritional"
get "/physical", to: "home#physical", as: "physical"
get "/intellectual", to: "home#intellectual", as: "intellectual"

Finally, I generated three new views to display the content itself (app/views/home/category_x.html.haml). I wrote the static content (in this case, just text and a linked button above the text to the sign up form) using Haml, which is quite similar to HTML. In fact, the easiest way from my point of view is to write HTML code and use an online converter to get the Haml code.

After that, I was able to easily add links to the the new pages from the homepage (/app/views/home/index.html.haml), for example, this portion of code corresponds to the Nutritional link in the homepage:

(...)
%p
  = image_tag("nutritional_icon.jpg")
%h3{style: "text-align: center;"}
 %a{href: "nutritional"}
   Nutritional
%p{style: "text-align: justify;"}
 Keep your body healthy and follow a better diet thanks to several interesting challenges...
#more-btn
 %a.btn{href: "nutritional"}
   more

1 comment:

  1. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. Email Extractor

    ReplyDelete