我在我的 Ruby on Rails 项目中使用acts_as_votable gem 来喜欢和不喜欢“Deals”。我的用户设置为act_as_voter,我的交易设置为acts_as_votable,但由于某种原因,一旦创建新用户,所有内容都设置为喜欢,并且他们无法与交易不同。出于某种原因,我的交易列表都有一个不同的按钮,它实际上并没有做任何事情,只是刷新页面。这是我的一些代码。
应用程序/视图/目录/index.html.erb
<ul class="deals_list">
<% @deals.each do |deal| %>
<li>
<div>
...
<div class="favorite">
<% if account_signed_in? and current_account.accountable_type == "Personnel" %>
<%= image_tag("dark-favorite.png") %>
<% if deal.liked_by current_account %>
<%= link_to unlike_deal_path(deal), method: :put do %>
Unlike
<% end %>
<% else %>
<%= link_to like_deal_path(deal), method: :put do %>
Like
<% end %>
<% end %>
<% end %>
</div>
</li>
<% end %>
</ul>
应用程序/控制器/deals_controller.rb
def like
@deal = Deal.find(params[:id])
@deal.liked_by current_account
redirect_back(fallback_location: catalog_index_url)
end
def unlike
@deal = Deal.find(params[:id])
@deal.unliked_by current_account
redirect_back(fallback_location: catalog_index_url)
end
配置/路由.rb
resources :deals do
member do
put 'like', to: "deals#like"
put 'unlike', to: "deals#unlike"
end
end