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

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

Rails6.0 | 民泊サイトの構築(改良版)| 28 | 予約モデルの実装 | for MacOSX



| 27 | Google認証の実装 <<  [ホーム] >> | 29 | 予約の実装(コントローラーとビュー作成)


コマンド
1文です。
rails g model Reservation user:references room:references start_date:datetime end_date:datetime price:bigint total:bigint


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


記述追加 app\models\room.rb
「has_many :reservations」の記述追加(5行目)

class Room < ApplicationRecord

  belongs_to :user

  has_many :reservations

  has_many_attached :photos
  has_rich_text :description

  geocoded_by :address
  after_validation :geocode, if: :address_changed?

  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



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

class User < ApplicationRecord

  has_many :rooms
  has_many :reservations

  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, :omniauthable

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

  def self.from_omniauth(auth)
    user = User.where(email: auth.info.email).first
    if user
      if !user.provider
        user.update(uid: auth.uid, provider: auth.provider, image: auth.info.image)
      end      
      return user
    else
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.email = auth.info.email
        user.password = Devise.friendly_token[0, 20]
        user.full_name = auth.info.name   # ユーザーモデルに名前があると仮定
        user.image = auth.info.image # ユーザーモデルに画像があると仮定
        user.uid = auth.uid
        user.provider = auth.provider
      end
    end
  end
  
end



記述追加 config\routes.rb
「resources :reservations, only: [:create]」の記述追加(28行目)

Rails.application.routes.draw do

  # ルートを app\views\pages\home.html.erb に設定
  root 'pages#home'

  devise_for :users, 
              path: '', 
              path_names: {sign_up: 'register', sign_in: 'login', edit: 'profile', sign_out: 'logout'},
              controllers: {registrations: 'registrations'}

  get 'pages/home'
  get '/dashboard', to: 'users#dashboard'
  get '/users/:id', to: 'users#show', as: 'user'
  
  post '/users/edit', to: 'users#update'

  resources :rooms, except: [:edit] do
    member do
      get 'listing'
      get 'pricing'
      get 'description'
      get 'photo_upload'
      get 'amenities'
      get 'location'
      delete :delete_photo
      post :upload_photo
    end
    resources :reservations, only: [:create]
  end

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end



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


| 27 | Google認証の実装 <<  [ホーム] >> | 29 | 予約の実装(コントローラーとビュー作成)

YAE C5 CLINIC(札幌美容クリニック)

関連記事(外部サイト)