1
0

More than 3 years have passed since last update.

【開発日記】薬価検索アプリno.5 〜Railsでアプリケーションの雛形作成、mySQLエラー、GitHubの設定まで〜

Posted at

前回の内容はこちら。
【開発日記】薬価検索アプリno.4 〜データベース設計〜

開発背景などはこちら
【開発日記】薬価検索アプリno.1 〜開発概要→企画〜

現在の進捗状況

1.必要な機能の洗い出し
2.必要な画面設計
3.データベース設計
4.Railsでアプリケーションの雛形作成 ⬅︎今ココ
5.大きな機能から順に実装
6.テストコードを書いて動作を担保する
7.リファクタリングして整理する
8.デプロイ

いよいよ立ち上げ

やっとコードを書き始める段階にきました。
今回はrails newしてrailsを使った開発を行っていきます。

開発環境

主な使用バージョンは以下です。

ruby 2.6.5
rails 6.0.0
mysql 5.6

rails new

ターミナルで以下を実行して新規Railsアプリけーションを立ち上げます。

#プロフェクトを立ち上げたいディレクトリに移って以下を実行。

$ rails _6.0.0_ new medipra -d mysql

よっしゃ、実行や!!

と勢いづいたらエラー発生。。。
Image from Gyazo

あと、gemファイルでもエラーが出ている。

まずは指示のある通り以下を実行してみた。

$ gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'

それでもダメだとおっしゃっている。。。


Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /Users/user/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
/Users/user/.rbenv/versions/2.6.5/bin/ruby -I /Users/user/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20191030-9501-185bh66.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/mysql@5.6/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... no
checking for MYSQL_OPT_SSL_ENFORCE in mysql.h... no
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/opt/mysql@5.6/lib
-----
creating Makefile

current directory: /Users/user/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/user/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/user/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2 for inspection.
Results logged to /Users/user/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/mysql2-0.5.2/gem_make.out

もうさすがにお手上げなので、エラー文からググって以下の記事を参照。

【Rails】MySQL2がbundle installできない時の対応方法
mysql2 gemインストール時のトラブルシュート
bundle install 時、mysql2でエラー

ざっくりまとめると、cppflagsとか、ldflagsのオプションをつければ良いらしい。(あぁ勉強不足。。。)

 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

で、そのあとに

$ bundle install

で、無事解決!!

バージョン管理はGitHubで!

これからコードを管理していくにあたって、バージョンの管理は必須です。転職するときにもGitを見られるそうですから、ぜひ、GitHubにリモートリポジトリを作成して管理していきましょう。

GitHub

登録がまだならこの際登録しましょう。
こちらです→GitHub

登録したらGitHub Desktopをインストールしておきます。
こちらから→GitHub Desktop

GitHubは色々できますし、チーム開発では必須です。(今回は一人で開発なんですけどね)

GitHub Desktop

ローカルリポジトリに追加

画面左上AddからAdd Existing Repositoryを選択して、Git管理したいファイル名を選択します。

コミットする

こんな画面になるのでファイルを全部選択してコミットします
Image from Gyazo

初回なのでinitial commitとして「Commit to master」をクリック。

リモートリポジトリに追加

コミットしたら、画面上にある「Publish repository」をクリック。こんなのが立ち上がるので、Nameつけて、Publish Repositoryをクリック。
Image from Gyazo
なお、keep this code privateは公開か非公開なのかの設定なのでお好みで。他の人にも見てもらうにはチェックを外しておく必要があります。

railsに関する設定おまけ

これからrails gコマンドを使ってコントローラーとか作ったりするわけですが、その際にマストじゃないファイルも一緒に作られてしまうので、あらかじめできないように設定することができます。
rails newで作ったファイルからconfig/application.rbを開き以下のように設定しましょう。

config/application.rb
module Medipra
  class Application < Rails::Application
    config.generators do |g|
      g.stylesheets false
      g.javascripts false
      g.helper false
      g.test_framework false
    end
  end
end

まとめ・所感

最後までお付き合いいただきありがとうございます。
今回は、rails newでRailsアプリケーションの雛形を作って、GitHubのリモート「リポジトリに登録するところまでやりました。

mysqlのエラーに関してはまだまだ理解不足なので、おすすめ本があれば教えてください!!

あ、それと、今回の薬価検索アプリの名前は メディプラ(medipra) にしました。深い理由はありません。
薬medicationの値段priceってだけです。
ちなみに、薬価はPharmaceutical Pricingだそうです(ウィキペディアより)

やーーーーーっと、ここから機能の開発を進めていきます。

次回、【開発日記】薬価検索アプリno.6 (後日追加します)に続きます。

1
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
1
0