Ruby on rails 链接到删除多态关联的链接?

Ruby on rails 链接到删除多态关联的链接?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,当我的型号为以下型号时,我无法确定如何删除我的产品订阅: class User has_many :products has_many :subscriptions, :foreign_key => :subscriber_id end class Product has_many :subscriptions, :as => :subscribable end class Subscription belongs_to :subscriber, :class_n

当我的型号为以下型号时,我无法确定如何删除我的产品订阅:

class User
  has_many :products
  has_many :subscriptions, :foreign_key => :subscriber_id
end

class Product
  has_many :subscriptions, :as => :subscribable
end

class Subscription
  belongs_to :subscriber, :class_name => "User"
  belongs_to :subscribable, :polymorphic => true
end
我尝试通过我的视图和控制器设置删除:

def unsubscribe_product
  @subscription = Subscription.find(params[:id])
  if @subscription.destroy
    redirect_to :back
  else
    redirect_to :back
  end
end

<td><%= link_to "Unsubscribe", { :controller => "products", 
                                 :action => "unsubscribe_product", 
                                 :id => subscription.id }, 
                                 :method => :delete %></td>
def退订产品
@subscription=subscription.find(参数[:id])
如果@subscription.destroy
重定向到:返回
其他的
重定向到:返回
结束
结束
“产品”,
:action=>“取消订阅产品”,
:id=>subscription.id},
:方法=>:删除%>
但是得到错误:

NameError in Pages#subscribe_area

undefined local variable or method `subscription' for #<#<Class> 
页面#订阅_区域中的
name错误

#的未定义局部变量或“subscription”方法看起来链接中的subscription应该是
@subscription
,表示您正在控制器中声明它。否则,我需要查看整个页面代码以及呈现它的操作

更新: 所以您没有定义订阅。请尝试以下方法:

<td><%= link_to "Unsubscribe", { :controller => "products", :action => "unsubscribe_product", :id => product.id }, :method => :delete %></td>

看起来链接中的订阅应该是
@subscription
,表示您正在控制器中声明它。否则,我需要查看整个页面代码以及呈现它的操作

更新: 所以您没有定义订阅。请尝试以下方法:

<td><%= link_to "Unsubscribe", { :controller => "products", :action => "unsubscribe_product", :id => product.id }, :method => :delete %></td>
def unsubscribe_product
  product = Product.find(params[:id])
  @subscription = product.subscriptions.find_by_subscriber_id(current_user.id)
  if @subscription.destroy
    redirect_to :back
  else
    redirect_to :back
  end
end