所以我有一个多态的收藏模型,它允许用户收藏各种不同的东西(目前工作正常)。我现在正在尝试销毁他们的收藏夹,但在选择要销毁的收藏夹时遇到困难,我不知道如何启动 form_for。
我的创建看起来像这样:
units_controller.rb - 在这里我为 form_for 创建了@favorite
@favorite = Favorite.new
@favorable = @unit
_favorite.html.erb
<%= form_for([@user, @favorable, @favorite]) do |f| %>
<div><%= f.hidden_field :favorer %></div>
<%= f.submit "Favorite", class: "btn btn-large btn-primary" %>
<% end %>
收藏夹控制器.rb
def create
@user = User.find(params[:user_id])
@favorite = @favorable.favorites.new(params[:favorite])
@favorite.favorer = current_user
@favorite.save # Favorite id: 1, favorer: 2, favorable_id: 1, favorable_type: "Unit"
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
到目前为止,这是我对删除的了解,但我不确定如何调用 form_for:
_unfavorite.html.erb
<%= form_for([@user, @favorable, @favorite], # problems here as @favorite is Favorite.new
html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unfavorite", class: "btn btn-large" %>
<% end %>
收藏夹控制器.rb
def destroy
@favorite = Favorite.find(params[:id])
@favorite.destroy
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
我的跟随模型使用这个,但我不知道如何将它嵌套在 /users/1/units/1/favorites
current_user.relationships.find_by_followed_id(@user)