LoginSignup
2
1

More than 5 years have passed since last update.

rails、君に決めた!!~2

Last updated at Posted at 2018-03-12

一連の記事の目次
rails、君に決めた!!~目次

前回の記事
rails、君に決めた!!~1

アプリ開発前の準備

サーバーの立ち上げ

mysqlを起動

mysql.server start
ちなみにシャットダウンは
(mysql.server stop)

config/database.ymlのdevelopmentのdatabaseをrails_appに変更

データベースを作成

bundle exec rake db:create

rake: (土などを)かいてならす、かき集める、念入りに探す

多義語すぎない?笑 なぜrakeなのかはよくわからない

サーバーの立ち上げ

bin/rails s

http://localhost:3000/で確認できる。
ところで、bin/railsってなんだっけってなった。

Rails4.1以降ではSpringというバックグラウンドでアプリケーションを実行することで、その待ち時間を短くする機能が搭載されている。これを使用するために、bundle exec の代わりにbin/を使用する

だそうです。

デバッグ

この本では2種類のデバッグ方法が紹介されている。
1. better_errorsを使う
Gemfileへの追記

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'
  gem 'better_errors' # 追加
  gem 'binding_of_caller' # 追加
end
bundle install

それでは意図的にエラーを起こす。今回はコントローラーから。
まずコントローラーの生成

bin/rails g controller home index

app/controllers/home_controller.rbへの追記

class HomeController < ApplicationController
  def index
    raise # 追加
  end
end
bin/rails s

で再度サーバーを立ち上げ、localhost:3000/home/indexにアクセス
image.png
better_errorsの画面でた!!
image.png
ここの部分が対話型開発環境(Read-Eval-Print Loop:REPLと略す)になってる。Django shellみたいなものか。
2. pry-byebugを使う

コンソールからstep by step でコードを実行できるデバッガー。

とりあえずbetter_errorsでは対応しきれない部分を補うデバッガーと理解しておく。。
Gemfileにgem 'pry-byebug'を追記してbundle install

デバッグしたいポイントの直前でbinding.pryと記述するとREPLが立ち上がるっぽい。

基本コマンド

1 rails generate

bin/rails g migrate ~~

bin/rails g model ~~

bin/rails g controller ~~

# 自動生成したファイルの削除
bin/rails destroy ~~

bin/rails g scaffold ~~

2 rails console

bin/rails c

デフォルトではirbコンソールが起動するが、pryを使いたいのでGemfileに
gem 'pry-rails'と追記しbundle installしておく。

設定

1 定数の設定

config/initializers/constants.rbを作成

# frozen_string_literal: true

module Constants
  SITE_NAME = 'Ruby on Rails'
end

bin/rails cでコンソールを立ち上げて確認してみる。

[1] pry(main)> Constants::SITE_NAME
=> "Ruby on Rails"

2 アプリケーションの設定
アプリケーション全体に共通する設定はconfig/application.rbに書く。
タイムゾーンとi18n(翻訳機能や多言語サポート機能)についての設定を追加する

module RailsApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Don't generate system test files.
    config.generators.system_tests = nil

    # タイムゾーンの追加設定
    config.time_zone = 'Tokyo'
    config.active_record.default_timezone = :local

    # i18nの追加設定
    config.i18n.available_locales = [:en, :ja]
    config.i18n.default_locale = :ja
  end
end
2
1
2

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
1