Reputation: 755
I'm trying to implement add to favorites and I'm getting this error:
ActiveRecord::RecordNotFound in FavoriteItemsController#index
Couldn't find Item with 'id'=
this is where the error pops up, at the following link:
<%= link_to "Favorite Items", favorites_path, method: :get, :class => 'navbar-link' %>
model associations
class Favorite < ApplicationRecord
belongs_to :viewer
belongs_to :favorited, polymorphic: true
end
class Viewer < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :favorites
has_many :favorite_items, through: :favorites, source: :favorited, source_type: 'Item'
end
routes:
get '/favorites', to: 'favorite_items#index', as: 'favorites'
resources :favorite_items, only: [:create, :destroy]
and the add
or remove
favorites links on my show page:
<%- unless current_shopper.favorite_items.exists?(id: @item.id) -%>
<%= link_to 'Add to favorites', favorite_items_path(item_id: @item), method: :post %>
<%- else -%>
<%= link_to 'Remove from favorites', favorite_item_path(@item), method: :delete %>
<%- end -%>
the controller
class FavoriteItemsController < ApplicationController
before_action :set_item
def index
@favorites = current_viewer.favorites
end
def create
if Favorite.create(favorited: @item, viewer: current_viewer)
redirect_to @item, notice: 'Item has been favorited'
else
redirect_to @item, alert: 'Something went wrong...*sad panda*'
end
end
def destroy
Favorite.where(favorited_id: @item.id, viewer_id: current_viewer.id).first.destroy
redirect_to @item, notice: 'Item is no longer in favorites'
end
private
def set_item
@item = Item.find(params[:item_id] || params[:id])
end
and the index to loop thru the favored items, although not sure if this is correct since the error stops before the loop:
<% @favorites.each do |item| %>
<tr>
<td><%= item.title %></td>
<td><%= item.price %></td>
<td><%= link_to 'Remove from favorites', favorite_items_path(@item.id), method: :delete %></td>
< /tr>
<% end %>
Update 1
This is what I get when I click on add to favorites
Item Exists (4.3ms) SELECT 1 AS one FROM "items" INNER JOIN "favorites"
ON "items"."id" = "favorites"."favorited_id" WHERE "favorites"."viewer_id"
= $1 AND "favorites"."favorited_type" = $2 AND "items"."id" = $3 LIMIT $4
[["viewer_id", 2], ["favorited_type", "Item"], ["id", 4], ["LIMIT", 1]]
items_controller.rb
def create
@item = Item.new(item_params)
respond_to do |format|
if @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: @item }
else
format.html { render :new }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
def show
@comments = Comment.where(item_id: @item).order("created_at DESC")
@items = Item.find(params[:id])
end
Upvotes: 0
Views: 2092
Reputation: 2675
class FavoriteItemsController < ApplicationController
before_action :set_item, only: [:create, :destroy]
..
..
end
Index html
<%= link_to 'Remove from favorites', favorite_items_path(@item.id), method: :delete %>
replace with
<%= link_to 'Remove from favorites', favorite_item_path(item.id), method: :delete %>
Upvotes: 1
Reputation: 797
Try replacing you FavoriteItemsController
with
before_action :set_item, :only => [:create, :destroy]
Upvotes: 0