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つのこと、
- サブスクライブするメソッドを作成します。
- リンクを正しくする。
これら2つをどのように実行しますか?