我正在开发一个ruby on rails app,作为投票并投票支持产品。代码工作唯一的问题是我需要通过javascript提交投票,我不知道如何开始这个。
继承我的路线
resources :products do
member do
post :vote_up
post :vote_down
end
end
heres是我的控制器文件
def vote_up
begin
@product = Product.find(params[:id])
current_user.vote_exclusively_for(@product)
@product.score = @product.plusminus
@product.save
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing =>true, :status => 404
end
end
def vote_down
begin
@product = Product.find(params[:id])
current_user.vote_exclusively_against(@product)
@product.score = @product.plusminus
@product.save
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing =>true, :status => 404
end
end
这是我的视图文件
<td><%= link_to "Vote up", vote_up_product_path(product), :method=>:post %></td>
<td><%= link_to "Vote down", vote_down_product_path(product), :method=>:post %></td>
javascript包含
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
答案 0 :(得分:2)
<td><%= link_to "Vote up", vote_up_product_path(product), :method=>:post, :remote => :true %></td>
然后,您可以处理将事件处理程序附加到该链接的响应:
$(the_link_selector)
.on('ajax:success', function1) // Executed when server answers with successful code
.on('ajax:error', function2) // Executed when server answers with error code