「Railsで1からWEBアプリケーションを作るぞ!」となったときに、まず何すればいいんだっけ?と忘れがち。いつもチュートリアルを振り返っている気がするので、備忘録メモを残します。
0. gem と rails のupdate
# gem のversion確認 gem -v # rails バージョン指定してインストール gem install rails -v 5.1.4 rails
1. 新規アプリケーションを作成
rails _5.1.4_ new アプリケーション名
-T
オプションをつけると、Test::Unit用のファイルを生成しない(RSpecを使用するために使う)
2. Gemfileを編集してインストール
Gemfileを編集
source 'https://rubygems.org' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" end # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.4' # Use Puma as the app server gem 'puma', '~> 3.7' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # 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.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 3.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development, :test do # Use sqlite3 as the database for Active Record gem 'sqlite3' # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] # Adds support for Capybara system testing and selenium driver gem 'capybara', '~> 2.13' gem 'selenium-webdriver' end group :development do # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 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 gem 'rails-controller-testing', '1.0.2' gem 'minitest-reporters', '1.1.14' gem 'guard', '2.13.0' gem 'guard-minitest', '2.4.4' end group :production do gem 'pg', '0.18.4' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
# gemのインストール(ローカルにいらないgemはインストールしない) bundle install --without production # gemを更新 bundle update
bundlerの実行
アプリケーションに必要なgemをインストール及びインクルードしていく
これを管理しているのがGemfile
PHPにおけるcomposer.jsonのようなもの
Gemfileにインストールしたいgemを書いていって、最後に以下のコマンドを叩く
bundle install
3. Gitのバージョン管理下に置く
git init git add -A git commit -m "Initialize repository" git remote add origin https://アカウント名@github.com/アカウント名/リモートリポジトリ名.git git push -u origin --all
4. herokuにデプロイ
RailsのデフォルトのページはHeroku上ではうまく表示されない仕様になっている
hello, wold! を出力してみる
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base protect_from_forgery with: :exception def hello render html: "hello, world!" end end
config/routes.rb
Rails.application.routes.draw do root 'application#hello' end
git commit -am "Add hello" git push heroku create git push heroku master
5. ログイン機能を実装
ホーム画面を作成
rails g controller StaticPages home
Source: Miningoo