LoginSignup
2
6

More than 5 years have passed since last update.

Railsでアプリの下準備をしてgithubに上げるまで(初学者、初心者向け)

Last updated at Posted at 2018-06-16

はじめに

railsでアプリを作る際の土台作りまとめです。global、local管理が初めは解り難いと思うので、その辺りをまとめてみました。
いつも使ういい感じのnewオプションとかがありましたらコメントで教えて頂けましたら幸いです。

環境

macOS HighSierra 10.13.4
Ruby 2.5.1
Rails 5.2.0

アプリケーションディレクトリを作成

$ cd desktop/workspace/
$ mkdir rails-app && cd $_
$ pwd
/Users/hogehoge/desktop/workspace/rails-app
  • && cd $_を付けると作成したディレクトリに移動もしてくれます。
  • pwdで現在に位置を確認できます。

作成したディレクトリでのRuby version の指定

$ rbenv versions
  system
  2.4.3
* 2.5.0 (set by /Users/hogehoge/.rbenv/version)
  2.5.1
$ rbenv local 2.5.1
$ rbenv version
2.5.1 (set by /Users/hogehoge/desktop/workspace/rails-app/.ruby-version)
  • rbenv virsionsで現在使用出来るpcにインストールされているrubyのversionを確認。*が現在使われている対象ver
  • rbenv local 2.5.0でディレクトリ内で使うRuby verを設定。rbenv globalで全体で使用するRuby verも設定出来ます。
  • この時点でpcに使用したいRuby verが入ってなかったら rbenv install Ruby-versionで対象のRubyをインストールして下さい。インストール出来るversionの一覧はrbenv install -listで確認出来ます。
  • rbenv versionで現在のversionが目的のものになっているか確認

gem Railsの準備

$ pwd
/Users/hogehoge/desktop/workspace/rails-app
$ bundle init
Writing new Gemfile to /Users/hogehoge/Desktop/workspace/rails-app/Gemfile
$ ls
Gemfile
  • pwdでapp-railsにいる事を確認した後に、bundle initでgem管理ツールbundleを使用する宣言をします。
  • ここでエラーが出たらbrew install bundlerでbundler自体をinstallてください。
  • lsコマンドでGemfileが出来ている事を確認。gem railsの記述部分を書き換えて対象のversionに指定してあげてください。
$ gem list rails
*** LOCAL GEMS ***

act-fluent-logger-rails (0.4.0)
coffee-rails (4.2.2)
factory_bot_rails (4.8.2)
jquery-rails (4.3.1)
rails (5.2.0, 5.1.4)
rails-controller-testing (1.0.2)
rails-dom-testing (2.0.3)
rails-html-sanitizer (1.0.4, 1.0.3)
rspec-rails (3.7.2)
sass-rails (5.0.7)
slim-rails (3.1.3)
sprockets-rails (3.2.1)

  • 最新版であるrails5.2.0がinstall出来る事を確認出来ましたので、gem 'rails', '~> 5.2.0'をGemfileに追加します。

bundle install

$ bundle install --path vendor/bundle --jobs=4
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Fetching rake 12.3.1
Fetching minitest 5.11.3
Fetching concurrent-ruby 1.0.5
Installing minitest 5.11.3
Installing rake 12.3.1
Installing concurrent-ruby 1.0.5
Fetching thread_safe 0.3.6
Fetching builder 3.2.3
Fetching erubi 1.7.1
Installing thread_safe 0.3.6
Fetching mini_portile2 2.3.0
Installing erubi 1.7.1
略
Installing sprockets-rails 3.2.1
Installing railties 5.2.0
Fetching rails 5.2.0
Installing rails 5.2.0
Bundle complete! 1 Gemfile dependency, 41 gems now installed.
Bundled gems are installed into `./vendor/bundle`
$ ls
Gemfile     Gemfile.lock    vendor
$ ls -a
.       .bundle     Gemfile     vendor
..      .ruby-version   Gemfile.lock
  • bundle installでgemをインストールします。
  • --path vendor/bundleオプションでgemをローカル管理(ディレクトリでの管理)をすることが出来ます。app-rails/vender/bundle以下にgemをインストールします。
  • --jobs=4オプションで並列処理をしてくれるので、早くインストール出来ます。
  • lsでvenderやGemfile.lookが出来ていることが確認できます。
  • ls -aで.ruby-version ( localのRuby-ver ) や.bundle ( bundleのinstall path ) が出来ていることを確認できます。
  • .bundleが出来ていますので、以降はbundle install時にpath指定は必要ありません。

Rails new

$ bundle exec rails new . -B

  • まず、bundle exec railsコマンドとただのrailsコマンドの違いですが、bundle execを使用する事により、ローカルのrailsコマンドを呼び出せますのでこちらの方がエラーが出にくいです。railsコマンドですとglobalでの汚染でエラーが出やすいので、エラーが出たらbundle execで試してください。
  • -B bundle installのスキップをします。bundle installは使用するgemをgemfileに記述した後に行いましょう。
  • ruby-versionでconflictが起きてしまうかもしれませんので、予めGemfileにはRuby-verを記載しておきましょう。

サーバーを立ち上げる

bundle exec rails s
=> Booting Puma
=> Rails 5.2.0 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.11.4 (ruby 2.5.1-p57), codename: Love Song
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
  • localサーバーが立ち上がります。適当なブラウザを立ち上げて、localhost:3000にアクセスしてみて下さい。以下の画面に移り変わったら成功です。
  • エラーが出たらbundle installを再度してみてください。
  • 今気づいたんですけどここでRubyとRailsのVer表示してくれてるんですね親切。ターミナルでCtrl-Cでサーバーは閉じます。 スクリーンショット 2018-06-16 12.24.50.png

設計図共有サイト()GitHubにあげる

  • まずGitHubにリポジトリーを作って上げてください。 スクリーンショット 2018-06-16 12.28.48.png スクリーンショット 2018-06-16 12.29.09.png
  • この手順でCreate!!!
  • その後ターミナルで下記コマンドでfirst commitを
$ echo "# rails-app" >> README.md
$ git init
Reinitialized existing Git repository in /Users/tashirosota/Desktop/workspace/rails-app/.git/
$ git add README.md
$ git commit -m "first commit"
[master (root-commit) 362ddea] first commit
 1 file changed, 25 insertions(+)
 create mode 100644 README.md
$ git remote add origin git@github.com:tashirosota/rails-app.git
$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 458 bytes | 458.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:tashirosota/rails-app.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

おわりに

今回のリポジトリはこちらで公開しております。 https://github.com/tashirosota/rails-app

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