Skip to content

予約、貸出formをJSで処理する形に変更#72

Open
yukimugikura wants to merge 1 commit intomainfrom
fix/reservation_lending_form
Open

予約、貸出formをJSで処理する形に変更#72
yukimugikura wants to merge 1 commit intomainfrom
fix/reservation_lending_form

Conversation

@yukimugikura
Copy link
Copy Markdown
Collaborator

  • 予約、貸出の処理をFormactionからJavascriptに変更
  • それに伴いform_withの編集
  • 一部controlでのActiveRecordの記述変更

メモ

  • 一部CSSが効かなくなりsubmitボタンなどのレイアウトが乱れてる
  • JSの書き方が自前なので機能はしているがコンソールでエラーメッセージが表示される

lending = @book.lendings.where(return_status: false, user_id: current_user.id).first
redirect_to lending if lending
reservation = @book.reservations.where("reservation_at >= ?", Time.now).where(user_id: current_user.id).first
@book = Book.eager_load(:reservation_active, :lend_active).where(id: params[:id]).with_attached_image.order("reservations.reservation_at asc").first
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@book = Book.eager_load(:reservation_active, :lend_active).where(id: params[:id]).with_attached_image.order("reservations.reservation_at asc").first
@book = Book.eager_load(:reservation_active, :lend_active).find_by(id: params[:id]).with_attached_image.order("reservations.reservation_at asc")

で取れたりします、、? 👀

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@book = Book.eager_load(:reservation_active, :lend_active).with_attached_image.order("reservations.reservation_at asc").find_by(id: params[:id])

で取得できました。ただ、このやり方だとeager_load とfind_by で2回クエリを発行するみたいです。

今の所は元のコードを使って、他のコントローラーを修正する時に、他の書き方がないか考えてみます

Comment on lines +26 to +27
@user = User.find(current_user.id)
@lending = @user.lendings.build(lending_params)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@user = User.find(current_user.id)
@lending = @user.lendings.build(lending_params)
@lending = current_user.lendings.build(lending_params)

で行けるかもです 🙏

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

たしかに

Comment on lines +16 to +17
@user = User.find(current_user.id)
@reservation = @user.reservations.build(reservation_param)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@user = User.find(current_user.id)
@reservation = @user.reservations.build(reservation_param)
@reservation = current_user.reservations.build(reservation_param)



<% if @reservations.exists? %>
<% if @book.reservation_active.any? %>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<% if @book.reservation_active.any? %>
<% if @book.reservation_active %>

でいい気もします! 👀

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@book.reservation_activeに該当する予約データがない時、その中身は空の配列、[]、になり
<% if @book.reservation_active %> がtrue になってしまいます。 それを回避するために.any?を付けています。

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど 🤔
ありがとうございます 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants