実現したいこと
bookmark機能の搭載を実行したいのですがうまくいきませんの
前提
railsにて簡易な旅行の予定アプリのようなものを作りそこにお気に入り機能を搭載させているのですが思ったようになりません。以下お願いします
発生している問題・エラーメッセージ
一般的な方法(bookmarkについて検索して見れる手法)だとこのように
例えばこれqiitaのBookmark情報
No route matches [GET] "/bookmarks"
自分の方法だとdb上及びviewとして登録はできるが一旦何かしらのエラーに
後述のindex_pageエラーか記述ミスのredirectの方です
該当のソースコード
config/routes.rb
1# frozen_string_literal: true 2 3Rails.application.routes.draw do 4 # planページ 5 resources :planners, only: %i[index new create show edit update destroy] do 6 resources :schedules, only: %i[index new create show edit update destroy] do 7 member do 8 get :bookmarks 9 end 10 end 11 end 12 resources :bookmarks, only: %i[index destroy] 13end
db/migrate/20250308130112_create_bookmarks.rb
1class CreateBookmarks < ActiveRecord::Migration[7.0] 2 def change 3 create_table :bookmarks do |t| 4 t.references :user, foreign_key: true 5 t.references :schedule, foreign_key: true 6 7 t.timestamps 8 end 9 add_index :bookmarks, [:user_id, :schedule_id], unique: true 10 end 11end
app/models/bookmark.rb
1# frozen_string_literal: true 2 3class Bookmark < ApplicationRecord 4 belongs_to :user 5 belongs_to :schedule 6 7 validates :user_id, uniqueness: { scope: :schedule_id } 8end
app/models/user.rb
1# frozen_string_literal: true 2 3class User < ApplicationRecord 4 5 has_many :schedules, dependent: :destroy 6 has_many :planners, dependent: :destroy 7 has_many :bookmarks, dependent: :destroy 8 has_many :bookmark_schedules, through: :bookmarks, source: :schedule 9 10 def own?(object) 11 object.user_id == id 12 end 13 14 # お気に入り 15 def bookmark(schedule) 16 bookmarks.create(schedule_id: schedule.id) unless bookmark?(schedule) 17 end 18 19 def unbookmark(schedule) 20 bookmarks.find_by(schedule_id: schedule.id)&.destroy 21 end 22 23 def bookmark?(schedule) 24 bookmarks.exists?(schedule_id: schedule.id) 25 end 26end
app/models/schedule.rb
1# frozen_string_literal: true 2 3class Schedule < ApplicationRecord 4 belongs_to :user 5 belongs_to :planner 6 has_many :bookmark_users, through: :bookmarks, source: :user 7 has_many :bookmarks, dependent: :destroy 8
app/views/schedules/_bookmark_buttons.html.erb
1<div class='ms-auto'> 2 <% if current_user.bookmark?(schedule) %> 3 <%= render 'unbookmark', { schedule: schedule } %> 4 <% else %> 5 <%= render 'bookmark', { schedule: schedule } %> 6 <% end %> 7</div>
app/views/schedules/_bookmark.html.erb
1<%= link_to bookmarks_path(schedule_id: schedule.id), 2 method: :post, 3 remote: true, 4 id: "bookmark-button-for-schedule-#{schedule.id}" do %> 5 <i class="bi bi-star"></i> 6<% end %>
app/views/schedules/_unbookmark.html.erb
1<%= link_to bookmark_path(bookmark), 2 data: { turbo_method: :delete }, 3 id: "unbookmark-button-for-schedule-#{schedule.id}" do %> 4 <i class="bi bi-star-fill"></i> 5<% end %>
app/controllers/bookmarks_controller.rb
1# frozen_string_literal: true 2 3class BookmarksController < ApplicationController 4 before_action :set_schedule, only: %i[index destroy] 5 6 def index 7 @bookmark = current_user.bookmarks.build(schedule_id: params[:schedule_id]) 8 @bookmark.save 9 redirect_to planner_schedules_path(planner) 10 end 11 12 def destroy 13 current_user.bookmarks.find_by(schedule_id: params[:schedule_id]).destroy! 14 end 15 16 private 17 18 def set_schedule 19 @schedule = Schedule.find(params[:schedule_id]) 20 end 21end 22
試したこと
ありふれた仕様なので基本的にはweb検索やAIも使いましたがコードはどこのパターンでも結局このルートエラーなのでroutes.rbを重点に検討しました
ただどうしてもエラーの解決が出来なかったので、当初resourcesの[create destroy]にindex(これはget扱いらしいので)を入れてたせいか1つだけonになってたので、試しにcreateをindexで使用したら([index destroy]の形で)登録そのものはできますがリダイレクト先がよくわからなくて
リダイレクト先指定=これも本来いらないみたいですが書かないと「No template for interactive request」のエラーが出る(これはindexページ作成で対処)
補足情報(FW/ツールのバージョンなど)
上記試した事のindexに関してですがそれでいいとして登録したものの一覧ページと区分けする問題はないのかなど気になっています、またついでですがとりあえずのルーティングです
rails routes | grep bookmark bookmarks_planner_schedule GET /planners/:planner_id/schedules/:id/bookmarks(.:format) schedules#bookmarks bookmarks POST /bookmarks(.:format) bookmarks#create bookmark DELETE /bookmarks/:id(.:format) bookmarks#destroy

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。