6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【図解あり】Rails6でBootstrapを導入してトップページを作成する

Last updated at Posted at 2021-02-22

Rails6でアプリケーションにBootstrapを導入した際の手順をまとめてみました。

完成形

cook.jpg
Bootstrapの導入までは以下をコピペ

##Bootstrapの導入

jQuery、Bootstrap、popper.jsの導入
ターミナル
yarn add jquery bootstrap popper.js
webpackの設定
config/webpack/environment.js
const { environment } = require('@rails/webpacker')

//ここから
const webpack = require('webpack')
environment.plugins.append(
  'Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery',
    Popper: ['popper.js', 'default']
  })
)
//ここまでを追加

module.exports = environment
javascriptフォルダ内にstylesheetsとapplication.scssを作成
ターミナル
mkdir app/javascript/stylesheets
touch app/javascript/stylesheets/application.scss
Bootstrap、CSSをインポート
app/javascript/packs/application.js
import 'bootstrap';
import '../stylesheets/application';
app/javascript/stylesheets/application.scss
@import '~bootstrap/scss/bootstrap';
アプリケーションのheadに以下を追記
app/views/layouts/application.html.erb
<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
画像を読み込ませる

バックグラウンドに画像を挿入する場合は作成してください

ターミナル
mkdir app/javascript/images

resolved_paths:[ ]の中に'app/javascript/images', 'app/assets/images'を記述

config/webpacker.yml
resolved_paths: ['app/javascript/images', 'app/assets/images']

導入完了

トップページの作成

#####コントローラーとビューの作成

ターミナル
rails g controller home index
config/routes.rb
Rails.application.routes.draw do
  root to: 'home#index'
end
ビューの編集
app/views/home/index.html.erb
<div class="jumbotron">
  <div class='index-content text-center'>
    <h1 class='home-index'>レシピを共有しよう!</h1>

    <div class='index-btn d-flex justify-content-around'>
      <%= link_to '新規登録', "#", class: "btn btn-success btn-lg" %>
      <%= link_to 'ログイン', "#", class: "btn btn-success btn-lg" %>
    </div>
  </div>
</div>

ここまででこんな感じ
top.png

#####CSSをあてる

ターミナル
touch app/javascript/stylesheets/home.scss
app/javascript/stylesheets/application.scss
@import '~bootstrap/scss/bootstrap';
@import './home.scss'; #追記

app/javascript/imagesに適当な画像を配置する
img.jpg

app/javascript/stylesheets/home.scss
.jumbotron{
  background-image: url('../images/cooking.jpg');
  background-size: cover;
  width: 100%;
  height: 100vh;
}

.home-index{
  font-weight: bold;
  font-size: 50px;
  color: purple;
  text-shadow:0 0 10px #FF0;
}

.index-content{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}

.index-btn{
  margin-top: 20px;
}

完成
cook.jpg

6
3
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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?