LoginSignup
1
2

More than 3 years have passed since last update.

Railsの勉強を初めて、基本的な内容をメモ

Last updated at Posted at 2019-04-15

はじめに

rails tutorialを終えて、整理するためにアウトプットします。
Qiitaに投稿するのも初めてなので、Markdownもよくわかりませんが、練習して見ようと思います。

Ruby on Railsをインストール

まずはRailsのインストール
下記のコマンドは、ざっくり説明すると、「Rubyドキュメントのインストールで無駄な時間を使わないように設定します」というものです。

$ printf "install: -N \nupdate: -N\n" >> ~/.gemrc

それでは早速、Railsをインストールします。
【コマンド実行】つぎのコマンドをターミナルで実行しましょう。

Railsのバージョンを指定してインストールするコマンド
$ gem install rails -v 5.1.6

アプリケーションの新規作成

rails _5.1.6_ new app

1.「5.1.6」はバージョン
2.「app」はアプリ名

Gemfileを修正して「bundle install」を実行し、gemをインストール
(Gemfileの内容は今後理解する必要あり)
エラーになった場合は「bundle update」する必要あり
(エラーのログに記載されている)

ローカル環境にインストールしないようにするためには下記の特殊なフラグを使用する
(HerokuでSQLiteはサポートされていないため、必要になるみたいだがよくわからない)

bundle install --without production

SSHの設定

ec2-userで実行
ssh-keygen -t rsa -C ""
何も入力せずEnter
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): 
パスワード設定
Enter passphrase (empty for no passphrase):

GitHub → setting → Deploy Keys(左側)をクリックしAdd newをクリック

$ cd .ssh
$ cat id_rsa.pub

表示されたコードをコピーし、貼り付ける

Herokuの設定

Heroku CLIのインストール(Herokuのコマンドラインインターフェイスを利用可能にする)

~/environment
$ curl -sL https://cdn.learnenough.com/heroku_install
$ source <(curl -sL https://cdn.learnenough.com/heroku_install)
$ echo 'PATH=/usr/local/heroku/bin:$PATH' >> $HOME/.bash_profile
$ source $HOME/.bash_profile > /dev/null

正しくインストールされたかは下記コマンドを実行

$ heroku --version

herokuに新しいアプリケーションの実行場所を作成する

$ heroku create

git&heroku コマンド

$ git checkout -b xxx
$ git add -A
$ git commit -m "コミットのメッセージ"
$ git checkout master
$ git merge 「ブランチ名」
$ git push
$ git push heroku
$ rails db:migrate
$ rails db:migrate:reset
$ heroku pg:reset DATABASE
$ heroku run rails db:migrate
$ heroku run rails db:seed
$ heroku restart

table 項目追加

rails generate migration add_index_to_users_email
(マイグレーションファイルに記述が必要)
rails generate migration add_password_digest_to_users password_digest:string
(マイグレーションファイルに記述不要)

Rails コマンド

フォルダやファイルの自動生成

home_controller.rbapp/controllersに生成され、このファイルの中身にtopメソッドがある
generateはgに省略が可能

$ rails generate controller home top

config/routes.rb内で「get "URL", to: 'コントローラ名#アクション名'」を記述することで top.html.erbの内容がブラウザで表示可能

get 'home/top', to: 'home#top'

アクションの追加方法

generateは生成するという意味なのでrails g ~ は使えません
ルーティングを追加し、アクションを追加する必要があります。

routes.rbにルーティングの設定

get 'about', to: 'home#about'

home_controller.rbにアクションの追加

def about
end

ビューを追加
app/views/homeabout.html.erbを追加

初期表示の変更

ルートURLを設定

root 'コントローラ名#アクション名'

コード

繰り返し処理

<% オブジェクト.each do |変数| %>

<% end %>

link_toメソッド

リンクをメソッド

<%= link_to "リンクの名前", URLまたはルーティングヘルパー %>

findメソッド

引数にidの値を指定することでそのidのレコードを取得する
Postテーブルの2レコード目を取得

post = Post.find(2)

redirect_toメソッド

更新処理等でページを移動する場合に使用
リダイレクトするときはpathではなくurlを使用する

redirect ヘルパー名_url

DB

データベースの操作

テーブル作成

マイレグレーションファイルを作成

$ rails g model Post content:text

Post・・・テーブル(モデルは単数形)
content:text・・・「カラム名:データ型」

ルーティングの確認方法

1.ターミナル
$ rails routers
2.ブラウザ
ブラウザのアドレスの末尾に/rails/infoと入力

本番環境の設定

SSL(Secure Sockets Layer)を使用
ローカルのサーバーから情報がネットワークに流れる前に、大切な情報を暗号化する技術です。
コメントアウトされている状態の可能性もあります。その場合はコメント解除でOK

config/environments/production.rb
Rails.application.configure do
  .
  .
  .
  # Force all access to the app over SSL, use Strict-Transport-Security,
  # and use secure cookies.
  config.force_ssl = true
  .
  .
  .
end
config/puma.rb
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/
  # deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end
./Procfile
web: bundle exec puma -C config/puma.rb
1
2
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
2