繁体   English   中英

Javascript act_as_votable gem

[英]Javascript acts_as_votable gem

好的,所以我使用acts_as_votable gem允许用户添加/删除集合中的书籍。 我希望添加/删除按钮能够在不重新加载页面的情况下工作,但找不到以这种方式实现按钮的任何地方,只是将其用作投票计数器。 我不在乎数票,只需添加/删除即可。 我的假设是我必须使用适当的javascript代码添加fave.js.erb和unfave.js.erb,但是究竟是什么呢? 提前致谢!

以下是控制器的操作:

def fave
@book = Book.find(params[:id])
current_user.likes @book
respond_to do |format|
  format.html { redirect_to :back }
  format.json
  format.js
end
end
def unfave
@book = Book.find(params[:id])
current_user.dislikes @book
respond_to do |format|
  format.html { redirect_to :back }
  format.json
  format.js
end
end

而routes.rb

resources :books do
get "book/:page", :action => :index, :on => :collection
member do
  post "fave", to: "books#fave"
  post "unfave", to: "books#unfave"
end
end

然后从视图中将它们称为:

<% if user_signed_in? %>
<ul class="list-inline text-center">
<% if current_user.voted_up_for? @book %>
<li><%= button_to unfave_book_path(@book, method: :put, remote: true), class: "btn btn-default btn-danger", title: "Remove from Collection", data: {disable_with: "<span class='glyphicon glyphicon-minus'></span> Removing..."} do %>
<span class="glyphicon glyphicon-heart-empty"></span>  Remove from Collection
<% end %>
</li>
<% else %>
<li><%= button_to fave_book_path(@book, method: :put, remote: true), class: "btn btn-default btn-success", title: "Add to Collection", data: {disable_with: "<span class='glyphicon glyphicon-plus'></span> Adding..."} do %>
<span class="glyphicon glyphicon-heart"></span>  Add to Collection
<% end %>
</li>
<% end %>

如果我了解您要正确执行的操作,则会根据用户的投票状态显示和隐藏按钮。 然后在用户添加/删除它们时显示/隐藏它们。

<ul class="list-inline text-center">
  <li data-book-id="<%= @book.id %>">
    <%= button_to unfave_book_path(@book, method: :put, remote: true), class: "btn btn-default btn-danger unfave #{'hidden' unless current_user.voted_up_for?(@book)}", title: "Remove from Collection", data: {disable_with: "<span class='glyphicon glyphicon-minus'></span> Removing..."} do %>
      <span class="glyphicon glyphicon-heart-empty"></span>  Remove from Collection
    <% end %>
    <%= button_to fave_book_path(@book, method: :put, remote: true), class: "btn btn-default btn-success fave #{'hidden' if current_user.voted_up_for?(@book)}", title: "Add to Collection", data: {disable_with: "<span class='glyphicon glyphicon-plus'></span> Adding..."} do %>
      <span class="glyphicon glyphicon-heart"></span>  Add to Collection
    <% end %>
  </li>
</ul>

我也假设你可能有多个li S代表不同的书。

fave.js.erb中 ,您可以执行以下操作:

var $li = $("li[data-book-id='<%= @book.id %>']");
$li.find('button.unfave').show();
$li.find('button.fave').hide();

unfave.js.erb中 ,您可以执行以下操作:

var $li = $("li[data-book-id='<%= @book.id %>']");
$li.find('button.fave').show();
$li.find('button.unfave').hide();

如果您真的想保持模板不变,则可以重新渲染部分模板:

$('ul.list-inline').html("<%= j render 'path/to/a/partial', book: @book %>");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM