kodamarisa / osinotameni_yasetai

0 stars 0 forks source link

ブックマークのアイコン表示について #50

Open kodamarisa opened 3 days ago

kodamarisa commented 3 days ago
  1. ブックマークボタンをクリックして、ブックマーク一覧に追加されるが、ページ再読み込みにて星が枠線に戻ってしまう。
  2. ブックマーク一覧でもブックマークされているのに枠線の星で表示される。
  3. ブックマーク一覧で削除しようとしても星が塗りつぶされた星に変わるだけで、削除されない。

app/controllers/exercises_controller.rb

class ExercisesController < ApplicationController
  def index
    logger.debug "INDEX: Current Calendar ID: #{session[:current_calendar_id]}"

    @calendar = Calendar.find_by(id: session[:current_calendar_id])
    unless @calendar
      logger.error "Calendar not found"
      flash[:alert] = 'カレンダーが見つかりません。'
      redirect_to calendars_path
      return
    end

    @q = Exercise.ransack(search_params)
    @exercises = @q.result
    logger.debug "Found Exercises: #{@exercises.map(&:name).join(', ')}"

    @exercise = @exercises.first if @exercises.any?
    @schedule = @calendar.schedules.new if @calendar
  end    

  def show
    logger.debug "SHOW: Exercise ID: #{params[:id]}, Calendar ID: #{session[:current_calendar_id]}"

    @calendar = Calendar.find_by(id: session[:current_calendar_id])
    unless @calendar
      logger.error "Calendar not found"
      flash[:alert] = 'カレンダーが見つかりません。'
      redirect_to calendars_path and return
    end

    @exercise = Exercise.find_by(id: params[:id])
    unless @exercise
      logger.error "Exercise not found"
      flash[:alert] = '筋トレが見つかりません。'
      redirect_to exercises_path and return
    end

    @schedule = @calendar.schedules.new(exercise: @exercise)
    logger.debug "Initialized New Schedule for Exercise: #{@exercise.name}"
  end  

  def autocomplete
    logger.debug "AUTOCOMPLETE: Query: #{params[:q]}"

    @exercises = Exercise.ransack(name_cont: params[:q]).result(distinct: true)
    logger.debug "Autocomplete Results: #{@exercises.map(&:name).join(', ')}"

    render json: @exercises.pluck(:name)
  end

  private

  def search_params
    logger.debug "SEARCH PARAMS: #{params[:q].inspect}"
    params.fetch(:q, {}).permit(:description_or_target_muscles_cont)
  end  
end

app/views/boolmarks/_already_bookmarked.js.erb

$("#bookmark-button-<%= @exercise.id %>").html('<%= icon "fas", "star" %>');
$("#bookmark-button-<%= @exercise.id %>").attr("href", "<%= exercise_bookmarks_path(@exercise) %>");
$("#bookmark-button-<%= @exercise.id %>").attr("data-method", "delete");

app/views/bookmarks/_exercise/row.html.erb

<div class="col-sm-12 col-lg-4 mb-3 exercise-card-<%= exercise.id %>">
  <div class="card">
    <div class="card-body">
      <h4 class="card-title d-flex justify-content-between align-items-center">
        <%= link_to exercise.name, exercise_path(exercise) %>
        <%= render 'exercises/bookmark', exercise: exercise %>
      </h4>
      <p class="card-text">
        <strong>Difficulty:</strong> <%= exercise.difficulty %><br>
        <strong>Duration:</strong> <%= exercise.is_cardio? ? "#{exercise.minimum_reps_or_distance} m" : "#{exercise.minimum_reps_or_distance} reps" %>
      </p>
    </div>
  </div>
</div>

app/views/bookmarks/create.js.erb

$("#bookmark-button-<%= @exercise.id %>").html('<%= icon "fas", "star" %>');
$("#bookmark-button-<%= @exercise.id %>").attr("href", "<%= exercise_bookmarks_path(@exercise) %>");
$("#bookmark-button-<%= @exercise.id %>").attr("data-method", "delete");

app/views/bookmarks/destroy.js.erb

$("#bookmark-button-<%= @exercise.id %>").html('<%= icon "far", "star" %>');
$("#bookmark-button-<%= @exercise.id %>").attr("href", "<%= exercise_bookmarks_path(@exercise) %>");
$("#bookmark-button-<%= @exercise.id %>").attr("data-method", "post");

ap/views/exercises/_bookmark.html.erb

<% if user_signed_in? || line_user_signed_in? %>
  <% current_user_or_line_user = user_signed_in? ? current_user : line_user %>
  <% if current_user_or_line_user&.bookmarks&.exists?(exercise: exercise) %> 
    <%= link_to '<i class="fa-solid fa-star"></i>'.html_safe, 
                exercise_bookmark_path(exercise, current_user_or_line_user.bookmarks.find_by(exercise: exercise)), 
                method: :delete, 
                remote: true, 
                id: "bookmark-button-#{exercise.id}", 
                class: "bookmark-btn btn btn-warning" %>
  <% else %>
    <%= link_to '<i class="fa-regular fa-star"></i>'.html_safe, 
                exercise_bookmarks_path(exercise), 
                method: :post, 
                remote: true, 
                id: "bookmark-button-#{exercise.id}", 
                class: "bookmark-btn btn btn-warning" %>
  <% end %>
<% end %>

app/views/exercises/_exercise_row.html.erb

<div class="col-sm-12 col-lg-4 mb-3">
  <div class="card">
    <div class="card-body">
      <h4 class="card-title d-flex justify-content-between align-items-center">
        <%= link_to exercise.name, exercise_path(exercise) %>
        <%= render 'exercises/bookmark', exercise: exercise %>
      </h4>
      <p class="card-text">
        <strong>Difficulty:</strong> <%= exercise.difficulty %><br>
        <strong>Duration:</strong> <%= exercise.is_cardio? ? "#{exercise.minimum_reps_or_distance} m" : "#{exercise.minimum_reps_or_distance} reps" %>
      </p>
      <%= link_to 'Add to Schedule', '#', class: "btn btn-success", 
            data: { bs_toggle: "modal", bs_target: "#scheduleModal", exercise_id: exercise.id } %>
    </div>
  </div>
  <!-- モーダル部分を読み込む -->
  <%= render 'schedule_modal', exercise: exercise %>
</div>

app/views/exercises/show.html.erb

<% if user_signed_in? || line_user_signed_in? %>
  <% current_user_or_line_user = user_signed_in? ? current_user : line_user %>
  <% if current_user_or_line_user&.bookmarks&.exists?(exercise: exercise) %> 
    <%= link_to icon("fas", "star").html_safe.html_safe, 
                exercise_bookmark_path(exercise, current_user_or_line_user.bookmarks.find_by(exercise: exercise)), 
                method: :delete, 
                remote: true, 
                id: "bookmark-button-#{exercise.id}", 
                class: "bookmark-btn btn btn-warning" %>
  <% else %>
    <%= link_to icon("far", "star").html_safe, 
                exercise_bookmarks_path(exercise), 
                method: :post, 
                remote: true, 
                id: "bookmark-button-#{exercise.id}", 
                class: "bookmark-btn btn btn-warning" %>
  <% end %>
<% end %>
kenchasonakai commented 3 days ago

ブックマークボタンをクリックして、ブックマーク一覧に追加されるが、ページ再読み込みにて星が枠線に戻ってしまう。

Bookmarkの登録が行われるが、リロードすると表示が意図したものになっていないのであれば、条件分岐やViewの見直しを行ったり検証するとよいかと思います。 1行・1メソッドすべて意味をきちんと理解して一つずつ見ていきましょう

<% if user_signed_in? || line_user_signed_in? %>
  <% current_user_or_line_user = user_signed_in? ? current_user : line_user %>
  <% if current_user_or_line_user&.bookmarks&.exists?(exercise: exercise) %> 
    <%= link_to '<i class="fa-solid fa-star"></i>'.html_safe, 
                exercise_bookmark_path(exercise, current_user_or_line_user.bookmarks.find_by(exercise: exercise)), 
                method: :delete, 
                remote: true, 
                id: "bookmark-button-#{exercise.id}", 
                class: "bookmark-btn btn btn-warning" %>
  <% else %>
    <%= link_to '<i class="fa-regular fa-star"></i>'.html_safe, 
                exercise_bookmarks_path(exercise), 
                method: :post, 
                remote: true, 
                id: "bookmark-button-#{exercise.id}", 
                class: "bookmark-btn btn btn-warning" %>
  <% end %>
<% end %>
kodamarisa commented 3 days ago

調べたら、current_user_or_line_userがnilであるためにブックマーク情報が取れていない状態でした。 なので、現在、ログイン動作でcurrent_user_or_line_userに値が渡されるかを確認しようとログアウトし、ログインをしようとしたところ、ログインにてユーザーに紐づいたカレンダーとそのカレンダーに紐づいたスケジュールが呼び出されず、 別の問題が出ています。なので、別のissuesを使用し、それについての質問を行います。