我需要幫助確定如何爲我的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
的ProductsController:
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
他們的2件事,
- 創建訂閱方法。
- 使鏈接正確。
我會怎麼做這兩個?