質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Q&A

0回答

676閲覧

ローカルでは正常に動く→Herokuで動かない

Randy_Tozuka

総合スコア19

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

0グッド

0クリップ

投稿2020/11/30 22:00

編集2022/01/12 10:55

単純なCRUD処理で会社の社食のテーブル予約のアプリを作成しています。
コードはとっ散らかってますが、なんとかローカル環境では動く事を何度も確認しています。
ところがこれをHerokuに上げたところ、create アクションの一部が動作しない状況となっています。
まったくリファクタリングしておらず、お恥ずかしいのですが、現状のcontrollerの記述は下記の通りで、この中で、#同じ人が同じ日に予約を入れる事を防ぐ…昼食は一日一回であろうと想定 …の箇所がなぜかHerokuでは動作せず、同じ人が同じ日に2つの予約を入れることが成功してしまいます。
同じ記述で、ローカルでできていることがステージング環境でできなくなるなんてことが発生しており、困っています。どなたかお答えいただける方がいらっしゃいましたらよろしくお願いいたします。

ちなみに heroku run rails db:migrate は実行済みです。

ruby

1class BookingsController < ApplicationController 2<略> 3 def create 4 @user = current_user 5 @booking_date = params[:booking][:date] 6 @booking_slot = params[:booking][:slot] 7 # 予約ができる日は本日以降の未来とする 8 if @booking_date < Date.today.to_s 9 flash[:danger]= "Head for the future!" 10 redirect_to new_booking_path 11 # 日曜(0)か土曜(6)は予約をさせない。→@booking_dateはstring型なのでdate型に変換し、wdayで曜日を出す 12 elsif Date.strptime(@booking_date).wday == 0 || Date.strptime(@booking_date).wday == 6 13 flash[:danger]= "Dont' be a slave of your job!" 14 redirect_to new_booking_path 15 else 16 #同じ人が同じ日に予約を入れる事を防ぐ…昼食は一日一回であろうと想定 17 if Booking.where(user_id: @user.id).where(date: @booking_date).any? 18 flash[:danger]= "Double booking in the the day! Please check." 19 redirect_to root_path 20 #同じ日の同じスロットに入る人数の調整…現在はテストとして1人以上予約が入ることを阻止 21 elsif Booking.where(date:@booking_date).where(slot:@booking_slot).count >= 1 22 flash[:danger]= "That slot is fully occupied! Please try other slot." 23 redirect_to new_booking_path 24 else 25 if current_user.bookings.create(booking_params) 26 flash[:success]= "Successfully booked" 27 redirect_to '/' 28 else 29 flash[:danger]= "Your booking failed" 30 redirect_to '/bookings/new' 31 end 32 end 33 end 34 end 35 36 def edit 37 @booking = current_user.bookings.find(params[:id]) 38 end 39 40 def update 41 @user = current_user 42 @booking_date = params[:booking][:date] 43 @booking_slot = params[:booking][:slot] 44 # 予約ができる日は本日以降の未来とする 45 if @booking_date < Date.today.to_s 46 flash[:danger]= "Head for the future!" 47 redirect_to root_path 48 # 日曜(0)か土曜(6)は予約をさせない。→@booking_dateはstring型なのでdate型に変換し、wdayで曜日を出す 49 elsif Date.strptime(@booking_date).wday == 0 || Date.strptime(@booking_date).wday == 6 50 flash[:danger]= "Dont' be a slave of your job!" 51 redirect_to root_path 52 else 53 # binding.pry 54 #同じ日の同じスロットに入る人数の調整…現在はテストとして1人以上予約が入ることを阻止 55 if Booking.where(date:@booking_date).where(slot:@booking_slot).count >= 1 56 flash[:danger]= "That slot is fully occupied! Please try other slot." 57 redirect_to root_path 58 else 59 if current_user.bookings.update(booking_params) 60 flash[:success]= "Successfully changed!" 61 redirect_to '/' 62 else 63 flash[:danger]= "Your booking changing failed" 64 redirect_to '/bookings/new' 65 end 66 end 67 end 68 end 69 70<略> 71 72 private 73 def booking_params 74 params.require(:booking).permit(:user_id, :date, :slot) 75 end 76 77end# of class 78

Modelは下記です。

ruby

1class Booking < ApplicationRecord 2 3 belongs_to :user 4 validates :user_id, presence: true 5 validates :date, presence: true 6 validates :slot, presence: true 7 # validate :slot_limit 8 default_scope -> {order(date: :asc)} 9 10 # def slot_limit 11 # binding.pry 12 # @booking_date = params[:booking][:date] 13 # @booking_slot = params[:booking][:slot] 14 # @bookings = Booking.where(date:@booking_date).where(slot:@booking_slot) 15 # if @bookings.count > 1 16 # errors.add(:base, 'That slot is fully occupied! Please try other slot.') 17 # end 18 # 19 # end 20 21end

一番最新のmigrationは下記です。

ruby

1class CreateBookings < ActiveRecord::Migration[6.0] 2 def change 3 create_table :bookings do |t| 4 t.datetime :date 5 t.string :slot 6 t.references :user, null: false, foreign_key: true 7 8 t.timestamps 9 end 10 #add_index :bookings, [:user_id] 11 end 12end 13

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2020/11/30 23:48

モデルとマイグレーションを提示してください。
Randy_Tozuka

2020/12/01 11:09

ありがとうございます! 質問を編集し、モデルとマイグレーションを追記いたしました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問