前提・実現したいこと
工程管理をメモできるアプリを作ってます。
views/static_pages/_user_logged_in.html.erbにブックマーク機能を表示させたいと思い、お気に入り機能実装中に下記のエラーが発生しました。
views/static_pages/_user_logged_in.html.erbをエラーなく、ブックマーク機能を表示させる為にはどうすればいいでしょうか。
教えて頂けると幸いです。宜しくお願いします。
発生している問題・エラーメッセージ
error
1ActionView::Template::Error (undefined local variable or method `taskmemo' for #<#<Class:0x00007f43ae138e88>:0x00007f43ae0ff368> 2Did you mean? @taskmemo): 3 1: <% if current_user.own?(taskmemo) %> 4 2: <%= render 'crud_menus', taskmemo: taskmemo %> 5 3: <% else %> 6 4: <%= render 'bookmark_area', taskmemo: taskmemo %> 7 8app/views/bookmarks/_form_bookmark.html.erb:1 9app/views/static_pages/_user_logged_in.html.erb:16 10app/views/static_pages/home.html.erb:2
該当のソースコード
######db/migrate/20210310204824_create_bookmarks.rb
ruby
1class CreateBookmarks < ActiveRecord::Migration[6.0] 2 def change 3 create_table :bookmarks do |t| 4 t.references :user, foreign_key: true 5 t.references :taskmemo, foreign_key: true 6 7 t.timestamps 8 end 9 add_index :bookmarks, [:user_id, :taskmemo_id], unique: true 10 end 11end
######db/schema.rb
ruby
1ActiveRecord::Schema.define(version: 2021_03_10_204824) do 2 3 # These are extensions that must be enabled in order to support this database 4 enable_extension "plpgsql" 5 6 ・ 7 ・ 8<省略> 9 ・ 10 ・ 11 12 create_table "bookmarks", force: :cascade do |t| 13 t.bigint "user_id" 14 t.bigint "taskmemo_id" 15 t.datetime "created_at", precision: 6, null: false 16 t.datetime "updated_at", precision: 6, null: false 17 t.index ["taskmemo_id"], name: "index_bookmarks_on_taskmemo_id" 18 t.index ["user_id", "taskmemo_id"], name: "index_bookmarks_on_user_id_and_taskmemo_id", unique: true 19 t.index ["user_id"], name: "index_bookmarks_on_user_id" 20 end 21 22 ・ 23 ・ 24<省略> 25 ・ 26 ・ 27 28 create_table "taskmemos", force: :cascade do |t| 29 t.string "product_name", limit: 30, null: false 30 t.string "order_number", limit: 6, null: false 31 t.datetime "delivery_date" 32 t.integer "quantity" 33 t.string "process" 34 t.text "remarks" 35 t.bigint "user_id" 36 t.datetime "created_at", precision: 6, null: false 37 t.datetime "updated_at", precision: 6, null: false 38 t.index ["user_id", "created_at"], name: "index_taskmemos_on_user_id_and_created_at" 39 t.index ["user_id"], name: "index_taskmemos_on_user_id" 40 end 41 42 create_table "users", force: :cascade do |t| 43 t.string "name", limit: 30, null: false 44 t.string "email", null: false 45 t.string "password_digest", null: false 46 t.integer "user_id" 47 t.datetime "created_at", precision: 6, null: false 48 t.datetime "updated_at", precision: 6, null: false 49 t.string "remember_digest" 50 t.string "activation_digest" 51 t.boolean "activated", default: false 52 t.datetime "activated_at" 53 t.string "reset_digest" 54 t.datetime "reset_sent_at" 55 t.boolean "admin", default: false 56 t.index ["email"], name: "index_users_on_email", unique: true 57 end 58 59 add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" 60 add_foreign_key "bookmarks", "taskmemos" 61 add_foreign_key "bookmarks", "users" 62 add_foreign_key "taskmemos", "users" 63end
######memo/app/models/bookmak.rb
ruby
1class SecondRelationship < ApplicationRecord 2 belongs_to :user 3 belongs_to :taskmemo 4 # user_id と board_idの組み合わせを一意性のあるものにしている 5 validates :user_id, uniqueness: { scope: taskmemo_id } 6end
######memo/app/controllers/bookmarks_controller.rb
ruby
1class BookmarkController < ApplicationController 2 3 def create 4 @taskmemo = Taskmemo.find(params[:taskmemo_id]) 5 current_user.bookmark(@taskmemo) # @taskmemoに 6 # redirect_to taskmemos_path, success: t('success') 7 end 8 9 def destroy 10 @taskmemo = current_user.bookmarks.find(params[:id]).taskmemo 11 current_user.unbookmark(@taskmemo) #@taskmemoに 12 end 13end
######memo/app/models/taskmemo.rb
class Taskmemo < ApplicationRecord belongs_to :user has_one_attached :image default_scope -> { order(created_at: :desc) } validates :user_id, presence: true ・ ・ <省略> ・ ・ has_many :bookmarks, dependent: :destroy ・ ・ <省略> ・ ・ def feed bookmarking_ids = "SELECT bookmarked_id FROM second_relationships WHERE bookmark_id = :taskmemo_id" Taskmemo.where("taskmemo_id IN (#{bookmarking_ids}) OR taskmemo_id = :taskmemo_id", taskmemo_id: id) end end
######memo/app/controllers/taskmemos_controller.rb
ruby
1class TaskmemosController < ApplicationController 2 before_action :logged_in_user, only: [:create, :destroy, 3 :bookmarked, :bookmarks] 4 before_action :correct_user, only: :destroy 5 6 7 def index 8 @taskmemos = current_user.taskmemos 9 end 10 11 def show 12 @taskmemo = current_user.taskmemos.find(params[:id]) 13 end 14 15 ・ 16 ・ 17<省略> 18 ・ 19 ・ 20 21 def bookmarks 22 @bookmark_taskmemos = current_user.bookmarks.includes(:user).order(created_at: :desc) 23 end 24 25 private 26 27 def taskmemo_params 28 params.require(:taskmemo).permit(:product_name, :order_number, 29 :delivery_date, :quantity, 30 :process, :remarks, :image) 31 end 32 33 def correct_user 34 @taskmemo = current_user.taskmemos.find_by(id: params[:id]) 35 redirect_to root_url if @taskmemo.nil? 36 end 37end 38
######memo/app/controllers/users_controller
ruby
1class UsersController < ApplicationController 2 before_action :logged_in_user, only: [:index, :edit, :update, :destroy, 3 :following, :follwers] 4 before_action :correct_user, only: [:edit, :update] 5 before_action :admin_user, only: [:index, :destroy] 6 7 ・ 8 ・ 9<省略> 10 ・ 11 ・ 12 13 def show 14 @user = User.find(params[:id]) 15 @taskmemos = @user.taskmemos.paginate(page: params[:page]) 16 end 17 18 19 private 20 21 def user_params 22 params.require(:user).permit(:name, :email, :password, 23 :password_confirmation) 24 end 25 26 # beforeアクション 27 28 def correct_user 29 @user = User.find(params[:id]) 30 redirect_to(root_url) unless current_user?(@user) 31 end 32 33 def admin_user 34 redirect_to(root_url) unless current_user.admin? 35 end 36end
######memo/app/models/user.rb
ruby
1 2 ・ 3 ・ 4<省略> 5 ・ 6 ・ 7 8 has_many :taskmemos, dependent: :destroy 9 10 ・ 11 ・ 12<省略> 13 ・ 14 ・ 15 16 has_many :bookmarks, dependent: :destroy 17 18 has_many :bookmarks_taskmemos, through: :bookmarks, source: :taskmemo 19 20 def own?(object) 21 id == object.user_id 22 end 23 24 ・ 25 ・ 26<省略> 27 ・ 28 ・ 29 30 def feed 31 following_ids = "SELECT followed_id FROM relationships 32 WHERE follower_id = :user_id" 33 Taskmemo.where("user_id IN (#{following_ids}) 34 OR user_id = :user_id", user_id: id) 35 end 36 37 ・ 38 ・ 39<省略> 40 ・ 41 ・ 42 43 def own?(object) 44 id == object.user_id 45 end 46 47 def bookmark?(taskmemo) 48 bookmark_taskmemos.include?(taskmemo) 49 end 50 51 def bookmark(taskmemo) 52 bookmark_taskmemos << taskmemo 53 end 54 55 def unbookmark(taskmemo) 56 bookmark_taskmemos.destroy(taskmemo) 57 end 58 59 ・ 60 ・ 61<省略> 62 ・ 63 ・ 64 65end
#####memo/app/controllers/static_pages_controller.rb
ruby
1class StaticPagesController < ApplicationController 2 def home 3 if logged_in? 4 @taskmemo = current_user.taskmemos.build 5 @feed_items = current_user.feed.paginate(page: params[:page]) 6 end 7 end 8 9 def help 10 end 11 12 def about 13 end 14 15 def contact 16 end 17end
######memo/app/views/static_pages/_user_logged_in.html.erb
html
1<div class="row"> 2 <aside class="col-md-4"> 3 <section class="user_info"> 4 <%= render 'shared/user_info' %> 5 </section> 6 <section class= "stats"> 7 <%= render 'shared/stats' %> 8 </section> 9 <section class="taskmemo_form"> 10 <%= render 'shared/taskmemo_form' %> 11 </section> 12 </aside> 13 <div class="col-md-8"> 14 <h3>製品一覧</h3> 15 <%= render 'shared/feed' %> 16 <%= render 'bookmarks/form_bookmark' %> 17 </div> 18</div>
######memo/app/views/bookmarks/_form_bookmark.html.erb
html
1<% if current_user.own?(taskmemo) %> 2 <%= render 'crud_menus', taskmemo: taskmemo %> 3<% else %> 4 <%= render 'bookmark_area', taskmemo: taskmemo %> 5<% end %>
######memo/app/views/bookmarks/_bookmark_area.html.erb
html
1<% if current_user.bookmark?(taskmemo) %> 2 <%= render 'unbookmark', taskmemo: taskmemo %> 3<% else %> 4 <%= render 'bookmark', taskmemo: taskmemo %> 5<% end %>
######memo/app/views/bookmarks/_bookmark.html.erb
html
1<%= link_to bookmarks_path(taskmemo_id: taskmemo.id), 2 id: "js-bookmark-button-for-taskmemo-#{taskmemo.id}", 3 class: "float-right", method: :post, remote: true do %> 4 <%= icon 'far', 'ster' %> 5<% end %>
######memo/app/views/bookmarks/_unbookmark.html.erb
html
1<%= link_to bookmark_path(current_user.bookmarks.find_by(taskmemo_id: taskmemo.id)), 2 id: "js-bookmark-button-for-taskmemo-#{taskmemo.id}", 3 class: "float-right", method: :delete, remote: true do %> 4 <%= icon 'fas', 'star' %> 5<% end %>
試したこと
userテーブル、taskmemosテーブル、bookmarkテーブルの一通りの関連づけ。
bookmark機能の一通りの実装。
補足情報(FW/ツールのバージョンなど)
ruby 2.7.1p83
Rails 6.0.3.4
Ubuntu 20.04 LTS