アプリケーションでthumbs_up gemを使用していますが、反復中にコントローラーに投票を保存する最良の方法を見つけようとしています。
これが私のモデルです:
class Vendor < ActiveRecord::Base
has_many :inventory_items
has_many :items, through: :inventory_items
has_many :shopping_lists, through: :inventory_items
acts_as_voteable
end
私の現在のコントローラー:
def vote_for_vendor
# not sure what to put in here
end
def vote_against_vendor
# not sure what to put in here
end
私の現在の見解:
<% provide(:title, 'Stores') %>
<table class="table table-condensed table-hover">
<tr>
<th>Name</th>
<th>Address</th>
<th>Favorite?</th>
</tr>
<% @vendors.each do |v| %>
<tr>
<td><%= v.name %></td>
<td><%= v.address %></td>
<td>
<% if current_user.voted_for(v) %>
<%= link_to 'unlike', vote_against_vendor_vendors_path(vendor_id: v.id), :remote => true %>
<% else %>
<%= link_to 'like', vote_for_vendor_vendors_path(vendor_id: v.id), :remote => true %>
<% end %>
</td>
</tr>
</table>
私が見た例のほとんどはparams([])
、関連情報をコントローラーに渡すために使用されていました。これはすべてのベンダーを表示するインデックス ページにすぎないため、実際にはパラメーターはありません。反復中にこの宝石を使用して投票を保存するにはどうすればよいですか? 前もって感謝します!
MrYoshiの助けを借りてコントローラーを更新
class VendorsController < ApplicationController
def index
@vendors = Vendor.all
end
def vote_for_vendor
vendor = Vendor.find(params[:vendor_id])
current_user.vote_for(vendor)
respond_to do |format|
format.js
end
end
def vote_against_vendor
vendor = Vendor.find(params[:vendor_id])
current_user.vote_against(vendor)
respond_to do |format|
format.js
end
end
end
私のルート:
resources :vendors do
collection { post :vote_for_vendor }
collection { post :vote_agaist_vendor }
end
現在のサーバー エラー
2013-09-06 10:07:29 -0700 に 127.0.0.1 の GET "/vendors/vote_for_vendor?vendor_id=4" を開始しました
AbstractController::ActionNotFound (VendorsController のアクション「表示」が見つかりませんでした):
…………
/Users/#Myname/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb をレスキュー/レイアウト内でレンダリング (0.5ms)