4:ほぼ静的なページの作成

サンプルアプリケーションを生成する
$ cd ..
$ pwd
/Rails/environment
$ rails _7.0.6_ new sample_app –skip-bundle
create
    create README.md
    create Rakefile
    create .ruby-version
   後略
$ cd sample_app/
Gmfileの編集
$ gedit Gemfile
source “https://rubygems.org”
git_source(:github) { |repo| “https://github.com/#{repo}.git” }

gem “rails”, “7.0.6”
gem “sassc-rails”, “2.1.2”
gem “sprockets-rails”, “3.4.2”
gem “importmap-rails”, “1.1.0”
gem “turbo-rails”, “1.1.1”
gem “stimulus-rails”, “1.0.4”
gem “jbuilder”, “2.11.5”
gem “puma”, “5.6.4”
gem “bootsnap”, “1.12.0”, require: false

group :development, :test do
gem “sqlite3”, “1.4.2”
gem “debug”, “1.5.0”, platforms: %i[ mri mingw x64_mingw ]
end

group :development do
gem “web-console”, “4.2.0”
end

group :test do
gem “capybara”, “3.37.1”
gem “selenium-webdriver”, “4.2.0”
gem “webdrivers”, “5.0.0”
gem “rails-controller-testing”, “1.0.5”
gem “minitest”, “5.15.0”
gem “minitest-reporters”, “1.5.0”
gem “guard”, “2.18.0”
gem “guard-minitest”, “2.4.6”
end

group :production do
gem “pg”, “1.3.5”
end
gemをインストール
$ bundle _2.3.14_ config set –local without ‘production’
$ bundle _2.3.14_ install
Fetching gem metadata from https://rubygems.org/………..
Resolving dependencies…….
Using rake 13.0.6
Using concurrent-ruby 1.2.2
後略

gitにコミット
$ git init
Reinitialized existing Git repository in /Rails/environment/sample_app/.git/
$ git add -A
$ git commit -m “Initialize repository”
[main (root-commit) 70d02f5] Initialize repository
70 files changed, 1235 insertions(+)
create mode 100644 .gitattributes
create mode 100644 .gitignore
create mode 100644 .ruby-version
create mode 100644 Gemfile
後略
README.md
を書き換えてみる
$ gedit README.md
# Ruby on Rails チュートリアルのサンプルアプリケーション

これは、次の教材で作られたサンプルアプリケーションです。
[*Ruby on Rails チュートリアル*](https://railstutorial.jp/)
(第7版)
[Michael Hartl](https://www.michaelhartl.com/) 著

ライセンス
後略
この変更をコミットする
$ git commit -am “Improve the README”
[main 7cba1a8] Improve the README
1 file changed, 37 insertions(+), 13 deletions(-)
helloアクションをApplicationコントローラーに追加する
$ gedit app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def hello
render html: “hello, world!”
end
end
ルーティングを設定する
$ gedit config/routes.rb
Rails.application.routes.draw do
  root “application#hello”
end
そしてコミット
$ git commit -am “Add hello”
[main b394c69] Add hello
2 files changed, 4 insertions(+), 4 deletions(-)
バンドルをロック
$ bundle _2.3.14_ lock –add-platform x86_64-linux
Writing lockfile to /Rails/environment/sample_app/Gemfile.lock
ブランチを作成
$ git switch -c static-pages
Switched to a new branch ‘static-pages’
StaticPagesコントローラを生成する
$ rails generate controller StaticPages home help
  create app/controllers/static_pages_controller.rb
  route get ‘static_pages/home’
    get ‘static_pages/help’
  invoke erb
後略
これでapp/controllers/static_pages_controller.rbにhomeとhelpが追加される

gitにコミット
$ git add -A
$ git commit -m “Add a Static Pages controller”
[static-pages 3020c96] Add a Static Pages controller
6 files changed, 28 insertions(+)
create mode 100644 app/controllers/static_pages_controller.rb
create mode 100644 app/helpers/static_pages_helper.rb
後略
そしてapp/controllers/static_pages_controller.rbを見てみると
class StaticPagesController < ApplicationController
 def home
 end

 def help
 end
end
とhomeとhelpのアクション(ブランクだが)が追加されている
ルートファイルにhomeとhelpの定義が追加されている。見てみよう
$ cat config/routes.rb
Rails.application.routes.draw do
 get ‘static_pages/home’
 get ‘static_pages/help’
 root “application#hello”
end
別ターミナルでserverが立ち上げっているのを確認後、http://localhost:3000/static_pages/homeにアクセスすると

このページはコントローラーのhomeによって呼び出されたapp/views/static_pages/home.html.erbの内容が表示されている
同様にhelpも同じ感じで表示される筈だ
HomeページのHTMLを修正する
$ gedit app/views/static_pages/home.html.erb
<h1>Sample App</h1>
<p>
  This is the home page for the
  <a href=”https://railstutorial.jp/”>Ruby on Rails Tutorial</a>
  sample application.
</p>
そしてブラウザからhttp://localhost:3000/static_pages/homeにアクセスすると

同様に
$ gedit app/views/static_pages/help.html.erb
<h1>Help</h1>
<p>
    Get help on the Ruby on Rails Tutorial at the
    <a href=”https://railstutorial.jp/help”>Rails Tutorial Help page</a>.
    To get help on this sample app, see the
   <a href=”https://railstutorial.jp/#ebook”><em>Ruby on Rails Tutorial</em>
    book</a>.
</p>
そしてブラウザから

フジマル について

1947年生れ、東京電機大学二部電気通信工学科卒、最後はスリランカ航空で営業だったのですが2018年に㈱インパック・ジャパンに再就職。趣味:登山、スキー、車いじり、コンピューター
カテゴリー: Ruby on railsに挑戦(almalinux9) パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください