마침내 아약스를 통해 올바르게 제출 된 투표가 완료되었지만 전혀 투표를 취소 한 것처럼 보일 수 있습니다. 여기 내 모델입니다 :Thumbs_up gem rails - 투표권을 다시 얻는 방법?
class User < ActiveRecord::Base
acts_as_voter
end
class Vendor < ActiveRecord::Base
acts_as_voteable
end
내 경로 :
resources :vendors do
collection do
post :vote_for_vendor
delete :vote_against_vendor
end
end
내 컨트롤러 :
class VendorsController < ApplicationController
def vote_for_vendor
begin
vendor = Vendor.find(params[:vendor_id])
current_user.vote_for(vendor)
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
def vote_against_vendor
begin
vendor = Vendor.find(params[:vendor_id])
current_user.vote_against(vendor)
render :nothing => true, :status => 404
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
내보기 :
<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 id="toggle">
<% if current_user.voted_for?(v) %>
<%= button_to "Unlike", { :controller => :vendors, :action => 'vote_against_vendor', :vendor_id => v.id},
{ :method => 'delete', :remote => true }%>
<% else %>
<%= button_to "Like", { :controller => :vendors, :action => 'vote_for_vendor', :vendor_id => v.id},
{ :method => 'create', :remote => true} %>
<% end %>
</td>
</tr>
<% end %>
</table>
그래서 공급 업체에 대한 unvote 수 있습니까? 나는 기본적으로 유권자가 한 번만 투표 할 수있는 thumbs_up 문서를 읽었습니다. 이것은 투표 한 사용자가 투표 할 수 없다는 것을 의미합니까? 구현의 목적은 좋아하는 버튼이나 비슷한 버튼을 갖는 것입니다.
는 여기에 내가 '과는 달리'클릭시 서버에서 받고 있어요 무엇 :
Started DELETE "/vendors/vote_against_vendor?vendor_id=1" for 127.0.0.1 at 2013-09-06 14:34:02 -0700
Processing by VendorsController#vote_against_vendor as JS
Parameters: {"authenticity_token"=>"/ZdePkPe+4rM8thUa+81hEL68cw1CJn93P0LQqEMC3s=", "vendor_id"=>"1"}
Vendor Load (0.2ms) SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1 [["id", "1"]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.1ms) begin transaction
Vote Exists (0.2ms) SELECT 1 AS one FROM "votes" WHERE ("votes"."voteable_id" = 1 AND "votes"."voteable_type" = 'Vendor' AND "votes"."voter_type" = 'User' AND "votes"."voter_id" = 1) LIMIT 1
(0.1ms) rollback transaction
Rendered text template (0.0ms)
Completed 404 Not Found in 7ms (Views: 0.6ms | ActiveRecord: 0.7ms | Solr: 0.0ms)
프로 팁 : 당신은 바보 살펴보기 전에 아주 철저하게 문서를 읽고 바보 후 질문을 부탁드립니다. 이 작업을 수행하려면 unvote_for
메소드를 사용하십시오. 내가 처음 어떻게 그것을 놓쳤는 지 잘 모르겠다. 당신이 vote_against
를 호출 한 후 vote_against_vendor
에서
진정한 단어를, 감사합니다. 나는 조금 바보 스럽다. 문서를 좀 더 자세히 살펴본 후에,'unvote_for' 메소드를 사용할 수있다. 매력처럼 일했습니다. 호기심에서, 나는이 요청을 원격으로 제출할 때 렌더링 호출이 거의 쓸모 없습니까? – settheline