1. Introduction
This post gathers information about how to create Static Pages in RAILS.2. Step by Step
2.1. Creating controller 'staticpages' for static pages
$ rails generate controller staticpages --skip-assetsRunning via Spring preloader in process 3573
create app/controllers/staticpages_controller.rb
invoke erb
create app/views/staticpages
invoke test_unit
create test/controllers/staticpages_controller_test.rb
invoke helper
create app/helpers/staticpages_helper.rb
invoke test_unit
2.2. Edit Controller 'staticpages' to implement actions
$ vim app/controllers/staticpages_controller.rb
class StaticpagesController < ApplicationController
def show
if valid_page?
render template: "staticpages/#{params[:page]}"
else
render file: "public/404.html", status: :not_found
end
end
private
def valid_page?
File.exist?(Pathname.new(Rails.root + "app/views/staticpages/#{params[:page]}.html.erb"))
end
end
def show
if valid_page?
render template: "staticpages/#{params[:page]}"
else
render file: "public/404.html", status: :not_found
end
end
private
def valid_page?
File.exist?(Pathname.new(Rails.root + "app/views/staticpages/#{params[:page]}.html.erb"))
end
end
2.3. Edit Routes for 'staticpages' and redirect root to staticpages/home
$ vim config/routes.rb
Rails.application.routes.draw do
:
get "/staticpages/:page" => "staticpages#show"
root 'staticpages#home'
root 'staticpages#home'
:
2.4. Edit some StaticPages: home, sitemap, userdoc, about
$ vim app/views/staticpages/home.html.erb
<h1>Static Page #Home</h1>
Find me in app/views/staticpages/home.html.erb
$ vim app/views/staticpages/sitemap.html.erb
<h1>Static Page #Sitemap</h1>
Find me in app/views/staticpages/sitemap.html.erb
$ vim app/views/staticpages/userdoc.html.erb
<h1>Static Page #Userdoc</h1>
Find me in app/views/staticpages/userdoc.html.erb
$ vim app/views/staticpages/about.html.erb
<h1>Static Page #About</h1>
Find me in app/views/staticpages/about.html.erb
Nenhum comentário:
Postar um comentário