LoginSignup
3
4

More than 3 years have passed since last update.

railsでほぼ静的なページの作り方

Posted at

引用先
Railsチュートリアル

※ rails new appをしている状況で新しくページを追加するところからです

ターミナルでコマンド実行

まずは作業ディレクトリへ移動

cd ~

移動後コマンド実行

$ rails generate controller <コントローラ名> <アクション名> <アクション名> ・・・

# 例
$ rails generate controller StaticPages home help

上記コマンドを実行すれば

・コントローラの作成
・ルーティングの設定
・ビューファイルの作成
を自動で作成してくれるので非常に便利かつ簡単ですね!

実際これだけでページは作れちゃいます。

新たにページを追加したいとなった時

例えば、新たにaboutページを作成したいとなった時に、手動でファイルを作成したりコードを書けば追加もできます。

手順

①ルーティングの設定
②コントローラにアクション追加
③ビューファイルの作成

①ルーティングの設定

config/routes.rb
Rails.application.routes.draw do
  get  'static_pages/home'
  get  'static_pages/help'
  get  'static_pages/about' ←追加
end

②コントローラにアクション追加

app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController

  def home
  end

  def help
  end

  def about ←追加
  end       ←追加
end

③ビューファイルの作成
app/views/static_pagesにabout.html.erbを右クリックで新規作成

app/views/static_pages/about.html.erb
<h1>About作成</h1>
・・・
・・・
・・・

これだけでページの追加は完了です。

URLは末にstatic_pages/aboutをつければwebで開きます。
 
(https://~~~/static_pages/about)

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4