我试图实施添加到收藏夹并且我收到此错误:
<%= 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]
路线:
add
以及我的展示页面上的remove
或<%- 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 %>
和循环通过喜欢的项目的索引,虽然不确定这是否正确,因为错误在循环之前停止:
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]]
更新1
这是我点击添加到收藏夹
时得到的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
items_controller.rb
function myDelete()
{
var ss = SpreadsheetApp.openById('***');
var allshts=ss.getSheets();
var sh=allshts[0];
var rg=sh.getDataRange();
var vA=rg.getValues();
for(var i=vA.length-1;i>=0;i--)
{
if(vA[i][8]==0 && vA[i][9]==0 && vA[i][10]==0 && vA[i][11]==0 && vA[i][12]==0 && vA[i][13]==0 && vA[i][14]==0 && vA[i][15]==0 && vA[i][16]==0 && vA[i][17]==0 && vA[i][18]==0 && vA[i][19]==0)
{
sh.deleteRow(i+1);
}
}
}
答案 0 :(得分:1)
class FavoriteItemsController < ApplicationController
before_action :set_item, only: [:create, :destroy]
..
..
end
索引HTML
&lt;%= link_to&#39;从收藏夹&#39;中删除, favorite_items_path(@ item.id),方法:: delete%&gt;
替换为
&lt;%= link_to&#39;从收藏夹&#39;,favorite_item_path(item.id)中删除, 方法::删除%&gt;
答案 1 :(得分:0)
尝试用{/ 1>替换FavoriteItemsController
before_action :set_item, :only => [:create, :destroy]