No route matches {:controller=>"guides", :action=>"favorite", :id=>nil, :format=>nil, :type=>"favorite"} missing required keys: [:id]
我正在尝试在我的应用程序中获取收藏列表。用户应该能够收藏和不受欢迎的指南。
指南控制器中最喜欢的操作:
def favorite
@guide = Guide.find(params[:id])
type = params[:type]
if type == "favorite"
current_user.favorites << @guide
redirect_to :back, notice: 'You favorited #{@guide.title}'
elsif type == "unfavorite"
current_user.favorites.delete(@guide)
redirect_to :back, notice: 'Unfavorited #{@guide.title}'
else
# Type missing, nothing happens
redirect_to :back, notice: 'Nothing happened.'
end
end
favorite_guide.rb
class FavoriteGuide < ActiveRecord::Base
belongs_to :guide
belongs_to :user
end
guide.rb
has_many :favorited_by, through: :favorite_guides, source: :user
的routes.rb
resources :guides do
member do
put "like", to: "guides#upvote"
put "dislike", to: "guides#downvote"
end
put :favorite, on: :member
end
以下是我在视图中打印出来的方式:
<%= link_to "favorite", favorite_guide_path(@guide, type: "favorite"), method: :put %>
<%= link_to "unfavorite", favorite_guide_path(@guide, type: "unfavorite"), method: :put %>
答案 0 :(得分:0)
通过更改
修复<%= link_to "favorite", favorite_guide_path(@guide, type: "favorite"), method: :put %>
<%= link_to "unfavorite", favorite_guide_path(@guide, type: "unfavorite"), method: :put %>
到
<%= link_to "favorite", favorite_guide_path(guide, type: "favorite"), method: :put %>
<%= link_to "unfavorite", favorite_guide_path(guide, type: "unfavorite"), method: :put %>