学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails6.0 | 動画学習サイトを作成| 05 | ナビゲーションバー(Bulma)

[04]MacOSXでのスタートアップ << [ホームに戻る] >> [06]デバイス(devise)


ナビゲーションバーを使えるようにBulmaの設定をします。
bulma.io


GemFileに以下の記述を追加します。


記述追加 GemFile(57行目)

gem 'bulma-rails', '~> 0.7.4'
gem 'bulma-extensions-rails', '~> 1.0.30'



GemFile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.1'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

# Bulma
gem 'bulma-rails', '~> 0.7.4'
gem 'bulma-extensions-rails', '~> 1.0.30'



bundleを使ってインストールします。


コマンド
bundle


「app\assets\stylesheets」フォルダにある「application.css」ファイルを「application.scss」にリネームします。
拡張子に「s」つけて「scss」にしています。



リネームした「application.scss」ファイルに以下の記述を追加します。


記述追加 app\assets\stylesheets\application.scss(17行目)

@import 'bulma';
@import 'bulma-extensions';



app\assets\stylesheets\application.scss(リネームしたファイル)

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
 * vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
 * files in this directory. Styles in this file should be added after the last require_* statement.
 * It is generally better to create a new file per style scope.
 *
 *= require_tree .
 *= require_self
 */

 @import 'bulma';
 @import 'bulma-extensions';



bulmを動かすにはjQueryが必要です。
jQueryはyarnを使ってインストールします。


コマンド
yarn add jquery@3.6.0


Webpackの設定をします。
「config\webpack\environment.js」ファイルに以下の記述を追加します。


記述追加 config\webpack\environment.js(5行目)

const webpack = require('webpack')
environment.plugins.append('Provide',
    new webpack.ProvidePlugin({
        $: 'jquery/src/jquery',
        jQuery: 'jquery/src/jquery',
        Popper: ['popper.js', 'default']
    })
)



config\webpack\environment.js

const { environment } = require('@rails/webpacker')

module.exports = environment

const webpack = require('webpack')
environment.plugins.append('Provide',
    new webpack.ProvidePlugin({
        $: 'jquery/src/jquery',
        jQuery: 'jquery/src/jquery',
        Popper: ['popper.js', 'default']
    })
)



一度サーバを起動します。


コマンド
rails s


サーバが起動したらブラウザで「http://localhost:3000/」にアクセスします。

ブラウザ確認
ブラウザ確認



ホームページのコントローラを作成しておきます。


コマンド
rails g controller Pages home


ナビゲーションバーを作成していきます。


「app\views」フォルダに「shared」フォルダを新規作成してください。
作成した「shared」フォルダに「_navbar.html.erb」ファイルを新規作成して下さい。



app\views\shared\_navbar.html.erb(新規作成したファイル)

<nav class="navbar is-link" role="navigation" aria-label="main navigation">
    <div class="navbar-brand">
        <a class="navbar-item" href="/">
            <h1>テストサイトGakushuu6</h1>
        </a>
        <a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
        </a>
    </div>

    <div id="navbarBasicExample" class="navbar-menu">
        <div class="navbar-end">
            <a class="navbar-item"></a>
            <a class="navbar-item"></a>
            <div class="navbar-item">
                <div class="buttons">
                    <a class="button is-info"><strong>新規ユーザ登録</strong></a>
                    <a class="button is-light">ログイン</a>
                </div>
            </div>
            <div class="navbar-item has-dropdown is-hoverable" style="margin-right: 100px;">
                <a class="navbar-item"></a>
                <div class="navbar-dropdown">
                    <a href="" class="navbar-item"></a>
                    <a href="" class="navbar-item"></a>
                    <hr class="navbar-divider">
                    <a href="" class="navbar-item">ログアウト</a>
                </div>
            </div>
        </div>
    </div>
</nav>

<script>
$(document).ready(function() {
  // navbar burgerアイコンでクリックイベントを確認する
  $(".navbar-burger").click(function() {
      // 「navbar-burger」と「navbar-menu」の両方で「is-active」クラスを切り替える
      $(".navbar-burger").toggleClass("is-active");
      $(".navbar-menu").toggleClass("is-active");
  });
});
</script>



「app\views\layouts\application.html.erb」ファイルに以下の記述を追加します。


記述追加 app\views\layouts\application.html.erb(14行目)

<%= render  "shared/navbar" %>



app\views\layouts\application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Gakushuu6</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>

    <%= render  "shared/navbar" %>

    <%= yield %>
  </body>
</html>



ブラウザ確認
http://localhost:3000/pages/home

ナビゲーションバー確認
ナビゲーションバー確認



ブラウザを縮小してスマートフォンでの表示を確認します。
右上にバーガーメニューがでます。

バーガーメニュー
バーガーメニュー



メニューを開いてみます。

メニューを開く
メニューを開く



↓↓クリックして頂けると励みになります。


[04]MacOSXでのスタートアップ << [ホームに戻る] >> [06]デバイス(devise)