0
0

More than 3 years have passed since last update.

railsチュートリアルをしていく~1章~

Last updated at Posted at 2019-10-30

概要

春にrailsチュートリアルをしていたのだが、夏に海外で2ヶ月遊んでからPCを触ることなく、怠惰の日々を送っていたため、何もかも忘れたので、今更必死にやっていく。今度は記事に書く。そしたら続けれるはず...

1章 hello_app

まず、hello_appアプリの作成

$ rails new hello_app
$ ls hello_app #hello_appの中身を確認
Gemfile       app/          db/           public/
Gemfile.lock  bin/          lib/          test/
README.md     config/       log/          tmp/
Rakefile      config.ru     package.json  vendor/

次にGemfileを編集する

$ cd hello_app #hello_appディレクトリに移動
$ open gemfile  #Gemfilを開ける
Gemfile
source 'https://rubygems.org'

gem 'rails',        '5.1.6'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.6.4'

group :development, :test do
  gem 'sqlite3',      '1.3.13'
  gem 'byebug', '9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           '3.5.1'
  gem 'listen',                '3.1.5'
  gem 'spring',                '2.0.2'
  gem 'spring-watcher-listen', '2.0.1'
end

bundle installでgemをインストール

$ bundle install
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
You have requested:
  spring = 2.0.2

The bundle currently has spring locked at 2.1.0.
Try running `bundle update spring`

If you are updating multiple gems in your Gemfile at once,
try passing them all to `bundle update`

bundle updateしろと言われたのでupdateする

$ bundle update

最後にRailsサーバーを実行する

$ rails s

http://localhost:3000/ にアクセスすると
スクリーンショット 2019-10-27 21.23.44.png

成功!!

Applicationコントローラにアクションhelloを追加

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

もう一度rails sをして http://localhost:3000/ にアクセスすると
スクリーンショット 2019-10-27 21.39.59.png

成功!!

git

自分の場合はソースをGithubにあげるだけ。
Bitbucket使うのはめんどくさい...

$ git init
$ git add -A
$ git commit -m "first commit"
$ git remote add origin https://github.com/tomo1227/hello_rails.git
$ git push origin master

heroku

gemfileに追加

gemfile
source 'https://rubygems.org'

gem 'rails',        '5.1.6'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.7.0'

group :development, :test do
  gem 'sqlite3', '1.3.13'
  gem 'byebug',  '9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           '3.5.1'
  gem 'listen',                '3.1.5'
  gem 'spring',                '2.0.2'
  gem 'spring-watcher-listen', '2.0.1'
end

group :production do
  gem 'pg', '0.20.0'
end
$ bundle install --without production
$ git commit -a -m "Update Gemfile for Heroku"
$ heroku --version
heroku/7.18.9 darwin-x64 node-v11.1.0
$ heroku update
$ heroku --version
heroku/7.33.3 darwin-x64 node-v11.14.0
$ heroku create hello-tomorails
Creating ⬢ hello-tomorails... done
https://hello-tomorails.herokuapp.com/ | https://git.heroku.com/hello-tomorails.git
$ git push heroku master

最後にhttps://hello-tomorails.herokuapp.com/ にアクセスすると・・・

スクリーンショット 2019-10-30 18.41.49.png

第1章 END

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