2
0

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 5 years have passed since last update.

【Rails】新規アプリケーションの作成準備

Last updated at Posted at 2019-12-21

まえがき

備忘のためのメモでもあるので、少し簡単に記載させていただいております。少しずつ肉付けできればとは考えておりますが、わかりにくいところなどありましたら申し訳ございません。ご質問やご指摘などメッセージいただきましたら私の方でもお調べしますので、共に前進できればと考えております。何卒よろしくお願い申し上げます。
また、TECH::EXPERTにて学習させていただいているため内容は参照させていただいております。

開発環境

MacOS:10.14.6
Ruby:2.5.1
Rails:5.2.4
MySQL:0.5.2

index

1.ターミナルで新規アプリケーションのディレクトリを作成
2.gemのバージョン指定を追記/変更
3.データベース作成 + ブラウザで確認

1.ターミナルで新規アプリケーションのディレクトリを作成

# 新規アプリケーションを作成したいディレクトリに移動
$ cd ~/ディレクトリ名

# 新規アプリをバージョンを5.2.3で、-dオプションでMySQLの使用を明示して作成
$ rails _5.2.4_ new アプリ名 -d mysql

# アプリ名ディレクトリに移動
$ cd アプリ名

# 現在のディレクトリのパスを表示(確認までに)
$ pwd

※以下のエラーが出る場合

Installing mysql2 0.5.2 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/user_name/Programs/web/foobar-repo/vendor/bundle/ruby/2.5.1/gems/mysql2-0.5.2/ext/mysql2
/Users/user_name/.rbenv/versions/2.5.1/bin/ruby -r (以下略)

バージョンに関するエラー
ターミナルで以下のコマンドを実行

$ gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/' -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib
$ cd ~/ディレクトリ名/アプリ名
$ bundle install

2.gemのバージョン指定を追記/変更

テキストエディタでアプリ名のディレクトリを開き、Gemfileから必要に応じてgemのバージョンを変更します。
私の場合は「mysql2」と「sass-rails」のバージョンに変更が必要でしたので以下で変更しました。
また一番下に新たにgemを3つ追記しております。

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

~中略~

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.4'
# Use mysql as the database for Active Record
gem 'mysql2', '0.5.2'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '5.0.7'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

~中略~

gem 'pry-rails'
gem 'compass-rails', '3.1.0'
gem 'sprockets', '3.7.2'

ターミナルで以下を実行し
Gemのバージョン指定を管理している「Gemfile.lock」というファイルを更新

# Gemfileを元にgemをインストールし、Gemfile.lockを更新する
$ bundle update

3.データベース作成

ターミナルで以下を実行

# データベースの作成
$ rails db:create

※ここまで終わったら以下のコマンドでサーバーを起動し
ブラウザ(http://localhost:3000)で確認

# サーバーを起動
$ rails s
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?