カレンダーの実装を、以前試した方法でやってみます。

カレンダーを実装する

今回はユーザー情報が結びついている日記を作成したいです。なので上記の実装だけではユーザー情報が結びついておらず、日記を保存することができません。

Started POST "/diaries" for ::1 at 2021-05-26 21:53:40 +0900
Processing by DiariesController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "diary"=>{"feeling"=>"🤓", "start_time(1i)"=>"2021", "start_time(2i)"=>"5", "start_time(3i)"=>"26", "body"=>"👩🏻‍💻"}, "commit"=>"Create Diary"}
  TRANSACTION (1.2ms)  BEGIN
  ↳ app/controllers/diaries_controller.rb:15:in `create'
  Diary Exists? (1.8ms)  SELECT 1 AS one FROM `diaries` WHERE `diaries`.`start_time` = '2021-05-26' LIMIT 1
  ↳ app/controllers/diaries_controller.rb:15:in `create'
  TRANSACTION (1.2ms)  ROLLBACK
  ↳ app/controllers/diaries_controller.rb:15:in `create'
Redirected to <http://localhost:3000/diaries>
Completed 302 Found in 22ms (ActiveRecord: 4.3ms | Allocations: 6213)

Started GET "/diaries" for ::1 at 2021-05-26 21:53:40 +0900
Processing by DiariesController#index as HTML
  Rendering layout layouts/application.html.erb
  Rendering diaries/index.html.erb within layouts/application
  Diary Load (1.8ms)  SELECT `diaries`.* FROM `diaries`
  ↳ app/views/diaries/index.html.erb:15
  Rendered simple_calendar/_month_calendar.html.erb (Duration: 16.7ms | Allocations: 7137)
  Rendered diaries/index.html.erb within layouts/application (Duration: 24.6ms | Allocations: 8639)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered shared/_flash_message.html.erb (Duration: 0.1ms | Allocations: 41)
  User Load (1.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  ↳ app/views/layouts/application.html.erb:15
  Rendered shared/_header.html.erb (Duration: 0.2ms | Allocations: 96)
  Rendered shared/_footer.html.erb (Duration: 0.2ms | Allocations: 39)
  Rendered layout layouts/application.html.erb (Duration: 37.3ms | Allocations: 14766)
Completed 200 OK in 39ms (Views: 34.7ms | ActiveRecord: 3.2ms | Allocations: 15294)

ユーザーIDを日記の情報に組み込むために行ったことは、diaries_controllerの編集です。現在ログインしているユーザーを紐づけていきます。

class DiariesController < ApplicationController
  def index
    @diaries = current_user.diaries
  end

  def new
    @diary = Diary.new
  end

  def show
    @diary = current_user.diaries.find(params[:id])
  end

  def create
    @diary = current_user.diaries.new(diary_params)
    if @diary.save
      redirect_to diaries_path, success: "#{@diary.feeling} in #{@diary.start_time}"
    else
      render :new
    end
  end

  def destroy
    @diary = current_user.diaries.find(params[:id])
    @diary.destroy
    redirect_to diaries_path, success:"it has beed deleted"
  end

  def edit
    @diary = current_user.diaries.find(params[:id])
  end

  def update
    @diary = current_user.diaries.find(params[:id])
    if @diary.update(diary_params)
      redirect_to diaries_path, success: "it has beed updated"
    else
      render :edit
    end
  end

  private

  def diary_params
    params.require(:diary).permit(:feeling, :body, :start_time)
  end
end

日記が保存される時にDiary Create (3.4ms) INSERT INTO diaries (feeling, body, start_time, user_id, created_at, updated_at) VALUES ('🤓', '👩🏻‍💻', '2021-05-25', 1, '2021-05-27 04:08:44.491488', '2021-05-27 04:08:44.491488')からみてもわかるとおりしっかりuser_idも入力されているのがわかりました。

Started POST "/diaries" for ::1 at 2021-05-27 13:08:44 +0900
Processing by DiariesController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "diary"=>{"feeling"=>"🤓", "start_time(1i)"=>"2021", "start_time(2i)"=>"5", "start_time(3i)"=>"25", "body"=>"👩🏻‍💻"}, "commit"=>"Create Diary"}
  User Load (2.1ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  ↳ app/controllers/diaries_controller.rb:15:in `create'
  TRANSACTION (0.9ms)  BEGIN
  ↳ app/controllers/diaries_controller.rb:16:in `create'
  Diary Exists? (1.0ms)  SELECT 1 AS one FROM `diaries` WHERE `diaries`.`start_time` = '2021-05-25' LIMIT 1
  ↳ app/controllers/diaries_controller.rb:16:in `create'
  Diary Create (3.4ms)  INSERT INTO `diaries` (`feeling`, `body`, `start_time`, `user_id`, `created_at`, `updated_at`) VALUES ('🤓', '👩🏻‍💻', '2021-05-25', 1, '2021-05-27 04:08:44.491488', '2021-05-27 04:08:44.491488')
  ↳ app/controllers/diaries_controller.rb:16:in `create'
  TRANSACTION (2.4ms)  COMMIT
  ↳ app/controllers/diaries_controller.rb:16:in `create'
Redirected to <http://localhost:3000/diaries>
Completed 302 Found in 28ms (ActiveRecord: 9.8ms | Allocations: 4517)

Started GET "/diaries" for ::1 at 2021-05-27 13:08:44 +0900
Processing by DiariesController#index as HTML
  User Load (1.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  ↳ app/controllers/diaries_controller.rb:3:in `index'
  Rendering layout layouts/application.html.erb
  Rendering diaries/index.html.erb within layouts/application
  Diary Load (1.1ms)  SELECT `diaries`.* FROM `diaries` WHERE `diaries`.`user_id` = 1
  ↳ app/views/diaries/index.html.erb:15
  Rendered simple_calendar/_month_calendar.html.erb (Duration: 11.2ms | Allocations: 6387)
  Rendered diaries/index.html.erb within layouts/application (Duration: 16.6ms | Allocations: 7802)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered shared/_flash_message.html.erb (Duration: 0.1ms | Allocations: 43)
  Rendered shared/_header.html.erb (Duration: 0.7ms | Allocations: 93)
  Rendered shared/_footer.html.erb (Duration: 0.2ms | Allocations: 39)
  Rendered layout layouts/application.html.erb (Duration: 41.2ms | Allocations: 13249)
Completed 200 OK in 47ms (Views: 40.9ms | ActiveRecord: 2.5ms | Allocations: 14279)

またログイン後のページをログインしたユーザー個人の日記一覧ページにする設定を行いました。

(user_sessions.controller.rb)

def create
    @user = login(params[:name], params[:password])
    if @user
      redirect_back_or_to diaries_path, success: t('.success')
    else
      flash.now[:danger] = t('.fail')
      render :new
    end
end

最初はredirect_back_or_to diaries_path(@user), success: t('.success')のように記入していたのですが、URLがlocalhost:3000/diaries.1のように表示されたので、@userでユーザー情報を送らないでログインしてみたらしっかり個人ページに送ることができました。これはdiary_controller.rbindexアクションで現在ログインしているユーザーを指定しているからです。

次にユーザーを変えても日記を投稿できるのか、他のユーザーの日記が表示されていないか確認してみました。

Started POST "/diaries" for ::1 at 2021-05-27 13:36:13 +0900
Processing by DiariesController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "diary"=>{"feeling"=>"😘", "start_time(1i)"=>"2021", "start_time(2i)"=>"5", "start_time(3i)"=>"27", "body"=>"🍰"}, "commit"=>"Create Diary"}
  User Load (2.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
  ↳ app/controllers/diaries_controller.rb:15:in `create'
  TRANSACTION (0.9ms)  BEGIN
  ↳ app/controllers/diaries_controller.rb:16:in `create'
  Diary Exists? (2.0ms)  SELECT 1 AS one FROM `diaries` WHERE `diaries`.`start_time` = '2021-05-27' LIMIT 1
  ↳ app/controllers/diaries_controller.rb:16:in `create'
  TRANSACTION (4.8ms)  ROLLBACK
  ↳ app/controllers/diaries_controller.rb:16:in `create'
  Rendering layout layouts/application.html.erb
  Rendering diaries/new.html.erb within layouts/application
  Rendered diaries/new.html.erb within layouts/application (Duration: 3.2ms | Allocations: 2133)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered shared/_header.html.erb (Duration: 0.8ms | Allocations: 94)
  Rendered shared/_flash_message.html.erb (Duration: 0.3ms | Allocations: 51)
  Rendered shared/_footer.html.erb (Duration: 0.1ms | Allocations: 39)
  Rendered layout layouts/application.html.erb (Duration: 25.8ms | Allocations: 7477)
Completed 200 OK in 49ms (Views: 27.1ms | ActiveRecord: 10.1ms | Allocations: 12102)

Diary Exists? (2.0ms) SELECT 1 AS one FROM diariesWHEREdiaries.start_time = '2021-05-27' LIMIT 1ここをみてわかる通り、start_timeは1つしか登録できません。ということで考えてみると、diariesテーブルを作り時にstart_timeカラムにunique制約をつけていたことを思い出しました。なので、このunique制約を外していきたいと思います。