2017-10-09 79 views
0

我想實現添加到收藏夾,我收到此錯誤:Rails的:無法與「ID」找項目=

ActiveRecord::RecordNotFound in FavoriteItemsController#index 
Couldn't find Item with 'id'= 

這是在錯誤彈出,在以下鏈接:

<%= link_to "Favorite Items", favorites_path, method: :get, :class => 'navbar-link' %> 

模型關聯

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 

路線:

get '/favorites', to: 'favorite_items#index', as: 'favorites' 
resources :favorite_items, only: [:create, :destroy] 

和我的節目頁面上的addremove收藏夾鏈接:

<%- 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 -%> 

控制器

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 

和指數環通的青睞的項目,雖然不知道這是正確的因爲錯誤在循環之前停止:

<% @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 %> 

更新1

這是當我點擊添加到收藏夾

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 
+0

你能否證實@item不是零? – krishnar

+0

在我的演出頁面上添加或刪除收藏夾鏈接:您的演出活動在哪裏? – krishnar

+0

@krishnar ...顯示動作在'items_controller.rb'中,並且在它裏面@ @ items = Item.find(params [:id])' – Theopap

回答

1
class FavoriteItemsController < ApplicationController 
    before_action :set_item, only: [:create, :destroy] 
    .. 
    .. 
end 

指數HTML

<%= link_to 'Remove from favorites', favorite_items_path(@item.id), method: :delete %>

代之以

<%= link_to 'Remove from favorites', favorite_item_path(item.id), method: :delete %>

0

嘗試用替換你FavoriteItemsController我得到什麼

before_action :set_item, :only => [:create, :destroy] 
+0

它的工作原理,但現在我在'index.html.erb'' <%= link_to'從收藏夾中刪除',favorite_items_path(@item。id),方法:: delete%>'錯誤是'未定義的方法ID'爲零:NilClass' – Theopap

+0

使用'item.id'而不是'@ item.id' –