【问题标题】:How to destroy my subscriptions?如何销毁我的订阅?
【发布时间】:2016-02-02 19:50:10
【问题描述】:

我有一个产品模型、一个用户模型和一个订阅模型。一个产品通过订阅有用户,一个用户通过订阅有很多产品,一个订阅属于两者。

当用户喜欢某个产品时,它会为此创建一个新订阅。在我的订阅控制器中,我为此创建了函数like_project;像魅力一样工作!

但我也尝试创建一个unlike-project 函数来销毁该特定订阅。我只是无法让它工作。我已经被告知要销毁关系而不是对象,但我已经认为订阅就是那个关系。

那么,我应该如何编写代码以确保用户也可以不喜欢它喜欢的产品(或销毁订阅)?

这是我的订阅控制器的功能:

class SubscriptionController < ApplicationController

before_action :authenticate_user!
    def like_product
    product = Product.find(params[:product_id])
    current_user.subscriptions.create(product: product)

    recommender = ProductRecommender.new
    recommender.add_to_matrix!(:users, "user-#{current_user.id}", "product-#{product.id}")

    redirect_to product     
end

def unlike_product
    @subscription = Subscription.find(params[:id])
    product = @subscription.product
    @subscription.destroy

    recommender = ProductRecommender.new
    recommender.delete_from_matrix!(:users, "product-#{product.id}")

    redirect_to product     
end

private
    def subscription_params
        params.require(:subscription).permit(:product_id, :user_id)
    end
end

这就是我喜欢产品的方式:

                <div class="card-action center">

                <% if user_signed_in? %>
                    <%= form_tag productsubscribe_path do %>
                        <%= hidden_field_tag 'product_id', @product.id %>
                        <button type="submit" class="btn waves-effect waves-light black darken-2">Like project</button>
                    <% end %>
                <% else %>
                    <%= link_to new_user_session_path do %>
                        <button type="submit" class="btn waves-effect waves-light black darken-2">Like project</button>
                    <% end %>
                <% end %>
                </div>

...这就是我试图与产品不同的方式:

                <div class="">
                <%= form_tag productunsubscribe_path do %>
                    <%= hidden_field_tag 'product_id', @product.id %>
                        <button type="submit" class="btn waves-effect waves-light black darken-2">Unlike product</button>
                <% end %>
                </div>

您可以看到的推荐代码是一个推荐引擎,它在产品被喜欢时将集合添加到 Redis 矩阵中,并在产品不受欢迎时将其删除。喜欢一个产品时不会引起任何问题,我假设它不会因为不喜欢一个产品而引起任何问题;)

谁能帮帮我?

【问题讨论】:

    标签: ruby-on-rails-4 destroy


    【解决方案1】:

    您的unlike_product 控制器方法期望使用params[:id] 来查找相关订阅。但是,在您的表单中,您似乎根本没有传入 id 参数 - 您拥有的唯一字段是 product_id

    <%= hidden_field_tag 'product_id', @product.id %>
    

    params[:id] 包含什么?是空的吗? params[:product_id] 怎么样?我们希望这不会是空的。所以而不是:

    @subscription = Subscription.find(params[:id])
    

    您将寻找 product_id 与您的参数匹配且 user_id 与登录用户匹配的订阅。

    【讨论】:

    • 谢谢@joshua。我想我明白你的意思,但我无法将它变成可行的代码。这是我有史以来的第一次破坏,我开始觉得自己很愚蠢;)我只是尝试了以下方法,但它不起作用:@subscription = Subscription.where("product_id = :id AND user_id = #{current_user.id}")
    • 试试@subscription = Subscription.where("product_id = #{params[:product_id]} AND user_id = #{current_user.id}")
    • 或者更好的是,使用占位符:Subscription.where("product_id = ? AND user_id = ?", params[: product_id], current_user.id)
    • 它为@subscription.destroy返回“错误数量的参数(0 代表 1)”
    • 到目前为止尝试了很多东西,但它仍然不起作用。不过,我认为您已将我引导到正确的方向,谢谢@joshua
    猜你喜欢
    • 2014-04-02
    • 2016-12-06
    • 2013-04-16
    • 1970-01-01
    • 2021-02-25
    • 2019-12-08
    • 2022-01-18
    • 1970-01-01
    • 2018-02-13
    相关资源
    最近更新 更多