title: Rails : Couldn't find Item with 'id'=
body: <p>I'm trying to implement add to favorites and I'm getting this error: </p>
  
  <pre><code> ActiveRecord::RecordNotFound in FavoriteItemsController#index
   Couldn't find Item with 'id'= 
  </code></pre>
  
  <p>this is where the error pops up, at the following link:</p>
  
  <pre><code>&lt;%= link_to "Favorite Items", favorites_path, method: :get, :class =&gt; 'navbar-link'  %&gt;
  </code></pre>
  
  <p>model associations </p>
  
  <pre><code>class Favorite &lt; ApplicationRecord
    belongs_to :viewer
    belongs_to :favorited, polymorphic: true
  end
  
  class Viewer &lt; ApplicationRecord
  devise :database_authenticatable, :registerable,
   :recoverable, :rememberable, :trackable, :validatable
  
  has_many :favorites
  has_many :favorite_items, through: :favorites, source: :favorited, source_type: 'Item'
  end
  </code></pre>
  
  <p>routes:</p>
  
  <pre><code>get '/favorites', to: 'favorite_items#index', as: 'favorites'
  resources :favorite_items, only: [:create, :destroy]
  </code></pre>
  
  <p>and the <code>add</code> or <code>remove</code> favorites links on my show page:</p>
  
  <pre><code>&lt;%- unless current_shopper.favorite_items.exists?(id: @item.id) -%&gt;
      &lt;%= link_to 'Add to favorites', favorite_items_path(item_id: @item), method: :post %&gt;
  &lt;%- else -%&gt;
      &lt;%= link_to 'Remove from favorites', favorite_item_path(@item), method: :delete %&gt;
  &lt;%- end -%&gt;
  </code></pre>
  
  <p>the controller</p>
  
  <pre><code>class FavoriteItemsController &lt; 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
  </code></pre>
  
  <p>and the index to loop thru the favored items, although not sure if this is correct since the error stops before the loop:</p>
  
  <pre><code> &lt;% @favorites.each do |item| %&gt;
      &lt;tr&gt;
       &lt;td&gt;&lt;%= item.title %&gt;&lt;/td&gt;
       &lt;td&gt;&lt;%= item.price %&gt;&lt;/td&gt;
       &lt;td&gt;&lt;%= link_to 'Remove from favorites', favorite_items_path(@item.id), method: :delete %&gt;&lt;/td&gt;
    &lt;  /tr&gt;
   &lt;% end %&gt;
  </code></pre>
  
  <p><strong>Update 1</strong></p>
  
  <p>This is what I get when I click on add to favorites </p>
  
  <pre><code>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]]
  </code></pre>
  
  <p>items_controller.rb</p>
  
  <pre><code>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
  </code></pre>