LoginSignup
0
0

More than 3 years have passed since last update.

RailsTutorial学習#2 第2章 Toyアプリケーション

Posted at

第2章 Toyアプリケーション

第2章Toyアプリケーション
2.1 アプリケーションの計画

$ cd ~/environment
$ rails _5.1.6_ new toy_app
$ cd toy_app/
$ bundle install --without production
もしうまく動かなかったらbundle updateを実行

最後に、GitでこのToyアプリケーションをバージョン管理下に置く

$ git init
$ git add -A
$ git commit -m "Initialize repository"

Bitbucketで新しいリポジトリを作成。
続いて、生成したファイルをこの新しいリモートリポジトリにプッシュ

$ git remote add origin git@bitbucket.org:<username>/toy_app.git
$ git push -u origin --all

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

続いてこの変更をコミットし、Herokuにプッシュ

$ git commit -am "Add hello"
$ heroku create
$ git push heroku master

2.2 Usersリソース

$ rails generate scaffold User name:string email:string

データベースをマイグレートする

$ rails db:migrate
== CreateUsers: migrating =================================
-- create_table(:users)
-> 0.0017s
== CreateUsers: migrated (0.0018s) ========================

$ rails server

2.2.1 ユーザーページを探検する
2.2.2 MVCの挙動

Railsルートで使うUsersリソース用のルール
config/routes.rb

Rails.application.routes.draw do
  resources :users
  root 'application#hello'
end
ルートからusersへのルーティングを追加する
config/routes.rb
Rails.application.routes.draw do
  resources :users
  root 'users#index'
end

2.2.3 Usersリソースの欠点
・ データの検証なし。 このままだと、ユーザー名が空欄だったり、でたらめなメールアドレスを入力したりしても通る。
・ ユーザー認証なし。 ログイン、ログアウトが行われていない。誰でも無制限に操作できる。
・ テストが書かれていない。 厳密にはこれは正しい表現ではありません。というのも、scaffoldで生成したコードにはごく簡単なテストが一応含まれているからです。ただ、scaffoldのテストコードはデータ検証やユーザー認証、その他の必要な要求を満たしていません。
・ レイアウトやスタイルが整っていない。 サイトデザインも操作法も一貫していません。
・ 理解が困難。 scaffoldのコードを理解できるぐらいなら、そもそも本書を読む必要はないでしょう。

2.3 Micropostsリソース

rails generate scaffold Micropost content:text user_id:integer

2.3.1 マイクロポストを探検する

rails db:migrate
Railsルートで使うMicropostsリソース用のルール
config/routes.rb
Rails.application.routes.draw do
  resources :microposts
  resources :users
  root 'users#index'
end

2.3.2 マイクロポストをマイクロにする

マイクロポストの最大文字数を140文字に制限する。
app/models/micropost.rb

class Micropost < ApplicationRecord
  validates :content, length: { maximum: 140 }
end

2.3.3 ユーザーはたくさんマイクロポストを持っている

1人のユーザーに複数のマイクロポストがある。
app/models/user.rb

class User < ApplicationRecord
  has_many :microposts
end

1つのマイクロポストは1人のユーザーにのみ属する。

app/models/micropost.rb
class Micropost < ApplicationRecord
  belongs_to :user
  validates :content, length: { maximum: 140 }
end
$ rails console
>> first_user = User.first
=> #<User id: 1, name: "Michael Hartl", email: "michael@example.org",
created_at: "2016-05-15 02:01:31", updated_at: "2016-05-15 02:01:31">
>> first_user.microposts
=> [#<Micropost id: 1, content: "First micropost!", user_id: 1,
created_at: "2011-11-03 02:37:37", updated_at: "2011-11-03 02:37:37">,
"2016-05-15 02:37:37", updated_at: "2016-05-15 02:37:37">,
#<Micropost id: 2, content: "Second micropost", user_id: 1,
created_at: "2016-05-15 02:38:54", updated_at: "2016-05-15 02:38:54">]
>> micropost = first_user.microposts.first
=> #<Micropost id: 1, content: "First micropost!", user_id: 1, created_at:
"2016-05-15 02:37:37", updated_at: "2016-05-15 02:37:37">
>> micropost.user
=> #<User id: 1, name: "Michael Hartl", email: "michael@example.org",
created_at: "2016-05-15 02:01:31", updated_at: "2016-05-15 02:01:31">
>> exit
マイクロポストのコンテンツが存在しているかどうかの確認
app/models/micropost.rb
class Micropost < ApplicationRecord
  belongs_to :user
  validates :content, length: { maximum: 140 },
                      presence: true
end
Userモデルに存在性のバリデーションを追加する
app/models/user.rb
class User < ApplicationRecord
  has_many :microposts
  validates FILL_IN, presence: true    # 「FILL_IN」をコードに置き換えてください
  validates FILL_IN, presence: true    # 「FILL_IN」をコードに置き換えてください
end

2.3.4 継承の階層
2.3.5 アプリケーションをデプロイする

$ git status
$ git add -A
$ git commit -m "Finish toy app"
$ git push
$ git push heroku

(上のコマンド操作では、2.1のHerokuアプリを作成済みであることが前提です。アプリを作成していないのであれば、先にheroku create、git push heroku masterを実行してから上のコマンド操作を実行してください。)

アプリケーションのデータベースを動作させるためには、次のheroku runコマンドを実行して本番データベースのマイグレーションを行う必要もあります (リスト 2.4)。

$ heroku run rails db:migrate
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