学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails6.0 | 民泊予約サイトの構築 | 19 | 部屋モデル作成

[18]プロフィールページ作成<< [ホームに戻る] >> [20]部屋コントローラ作成


コマンド
長いですが全部つながっています。1文です。
rails g model Room home_type:string room_type:string accommodate:bigint bed_room:bigint bath_room:bigint listing_name:string summary:text address:string is_tv:boolean is_kitchen:boolean is_air:boolean is_heating:boolean is_internet:boolean price:bigint active:boolean user:references


「db\migrate\20200724093933_create_rooms.rb」ファイルの記述を以下のように変更します。


記述更新 db\migrate\20200724093933_create_rooms.rb
18行目に「, default: false」の記述を追加しています。

class CreateRooms < ActiveRecord::Migration[6.0]
  def change
    create_table :rooms do |t|
      t.string :home_type
      t.string :room_type
      t.bigint :accommodate
      t.bigint :bed_room
      t.bigint :bath_room
      t.string :listing_name
      t.text :summary
      t.string :address
      t.boolean :is_tv
      t.boolean :is_kitchen
      t.boolean :is_air
      t.boolean :is_heating
      t.boolean :is_internet
      t.bigint :price
      t.boolean :active, default: false
      t.references :user, null: false, foreign_key: true

      t.timestamps
    end
  end
end



コマンド マイグレーション
rails db:migrate


記述追加 app\models\user.rb
「has_many :rooms」の追加(3行目)

class User < ApplicationRecord

  has_many :rooms

  has_one_attached :avatar
  
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :confirmable

  validates :full_name, presence: true, length: {maximum: 50}
end



app\models\room.rb
バリデーションの記述追加

class Room < ApplicationRecord

  belongs_to :user

  validates :home_type, presence: true
  validates :room_type, presence: true
  validates :accommodate, presence: true
  validates :bed_room, presence: true
  validates :bath_room, presence: true
  
end



↓↓クリックして頂けると励みになります。


[18]プロフィールページ作成<< [ホームに戻る] >> [20]部屋コントローラ作成