Acts_As_Votable Gem 自动点赞所有内容
Acts_As_Votable Gem Automatically Liking Everything
我在 Rails 项目的 Ruby 中使用 acts_as_votable gem 喜欢和不喜欢 "Deals"。我的用户设置为 act_as_voter,我的交易设置为 acts_as_votable,但出于某种原因,一旦创建新用户,一切都设置为喜欢,并且他们无法取消交易。出于某种原因,我的交易列表都有一个不同的按钮,它实际上除了刷新页面之外什么也没做。这是我的一些代码。
app/views/catalog/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>
app/controllers/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
config/routes.rb
resources :deals do
member do
put 'like', to: "deals#like"
put 'unlike', to: "deals#unlike"
end
end
请务必阅读整个自述文件,因为您使用的库有误。
To check if a voter has voted on a model, you can use voted_for?. You can check how the voter voted by using voted_as_when_voted_for.
我关注你的问题是因为我期待看到一个“?”在 deal.liked_by 调用之后,这将指示布尔结果(按照惯例,并非总是如此)。
所以改用这个:
<% if current_account.voted_for? deal %>
我在 Rails 项目的 Ruby 中使用 acts_as_votable gem 喜欢和不喜欢 "Deals"。我的用户设置为 act_as_voter,我的交易设置为 acts_as_votable,但出于某种原因,一旦创建新用户,一切都设置为喜欢,并且他们无法取消交易。出于某种原因,我的交易列表都有一个不同的按钮,它实际上除了刷新页面之外什么也没做。这是我的一些代码。
app/views/catalog/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>
app/controllers/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
config/routes.rb
resources :deals do
member do
put 'like', to: "deals#like"
put 'unlike', to: "deals#unlike"
end
end
请务必阅读整个自述文件,因为您使用的库有误。
To check if a voter has voted on a model, you can use voted_for?. You can check how the voter voted by using voted_as_when_voted_for.
我关注你的问题是因为我期待看到一个“?”在 deal.liked_by 调用之后,这将指示布尔结果(按照惯例,并非总是如此)。
所以改用这个:
<% if current_account.voted_for? deal %>