Railsにこれから初めて触れる方を対象にしたチュートリアルです
Comfortable Mexican Sofa
というCMSをRailsへ導入するチュートリアルです
まず、rails new
を実行し、Railsアプリのひな型を作成します。
rails new cms
次に、作成したRailsアプリのディレクトリへと移動します。
cd cms
まず、Gemfile
にgem "comfortable_mexican_sofa", "~> 2.0.0"
を追加します
gem "comfortable_mexican_sofa", "~> 2.0.0"
その後、bundle install
を実行します
bundle install
Comfortable Mexican Sofa
をインストールします
rails generate comfy:cms
最後に、rails db:migrate
を実行します
rails db:migrate
後は、rails s
を実行してlocalhost:3000/admin
にアクセスします
ユーザー名とパスワードを要求されるので以下のように入力します
ログインできればOKです!
これでComfortable Mexican Sofa
の導入は完了です!
ログインするとサイトを作成する画面に切り替わります 以下の項目を入力して、「Create Site」をクリックします
クリック後、「新規レイアウト」作成画面に切り替わります 以下の項目を入力して、「レイアウトを作成」をクリックします
次に、画面左のメニュー内にある「ページ」を選択し、以下のように入力します
「ページを作成」をクリックしページを作成します
最後に、ページとレイアウトが作成できているか確認します
画面左側にある「サイト」をクリックし、localhost:3000/sample
にアクセスします
ブラウザに以下のように表示されていればページとレイアウトが作成されています
Hogehoge
Hello Comfortable Mexican Sofa!
このままではデザインなどが簡素すぎるのでBootstrap
を使いたいと思います。
まず、Gemfile
にgem 'bootstrap', '~> 4.2.1'
とgem 'jquery-rails'
を追加し、bundle install
します。
gem 'bootstrap', '~> 4.2.1'
gem 'jquery-rails'
bundle install
もし、gem
の依存関係でうまくいかない場合は、bundle update
を実行してからbundle install
を実行してください
bundle update
bundle install
その後、app/assets/javascripts/application.js
とapp/assets/stylesheets/application.scss
を下記のように変更します
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require activestorage
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require turbolinks
//= require_tree .
/*
* 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 "bootstrap";
その後、config/boot.rb
を以下のように修正します
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
ENV['EXECJS_RUNTIME'] = 'Node'
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
これでBootstrap
が使用できるようになりました。
最後に、レイアウトからheader
レイアウトを下記のように編集します
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a href="/sample" class="navbar-brand">CMS</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Menu
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a href="/sample" class="dropdown-item">Home</a>
</div>
</div>
</nav>
「レイアウトを更新」をクリックするとレイアウトが修正後の内容に更新されます
localhost:3000/sample
でナビゲーションバーが表示されていればOKです