我需要帮助来确定如何为我创建一个链接Product
,使用户能够订阅它。我首先有我的Subscription
模型:
class Subscription < ActiveRecord::Base
attr_accessible :subscribable_id
belongs_to :subscriber, :class_name => "User"
belongs_to :subscribable, :polymorphic => true
end
然后我的Product
模型:
class Product < ActiveRecord::Base
attr_accessible :name, :price
belongs_to :user
has_many :subscriptions, :as => :subscribable
end
我的计划是制作我的视图,类似于DELETE
单击以订阅产品的链接的方法。这是我的路线,控制器然后查看:
resources :products do
post :subscribe_product, :on => :collection
end
产品控制器:
def subscribe_product
@product = Product.find(params[:id])
# Not sure what goes here next?
# Something like: user.subscriptions.create(:subscribable => product)
end
看法:
<table>
<% for product in @products %>
<tbody>
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= link_to 'Delete', product, :confirm => 'Are you sure?', :method => :delete %></td>
<td><%= link_to 'Subscribe', :controller => "products", :action => "subscribe_product", :id => product.id %></td>
</tr>
</tbody>
<% end %>
</table>
现在这给出了一个奇怪的错误:
ActiveRecord::RecordNotFound in ProductsController#show
Couldn't find Product with id=subscribe_product
他们的两件事,
- 创建订阅方法。
- 使链接正确。
我该怎么做这两个?