2013-05-12 48 views
0

我正在開發一款Ruby應用程序,它可以投票並投票選擇產品。代碼工作唯一的問題是我需要投票通過JavaScript提交,我不知道如何開始這一點。使用JavaScript來關注鏈接

我的繼承人路線

resources :products do 
    member do 
     post :vote_up 
     post :vote_down 
    end 
end 

繼承人是我的控制器文件

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 %> 

回答

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 
+0

這似乎並不奏效。也許它的東西與我的JavaScript包括? – Tyler 2013-05-12 16:33:44

+0

nvm我現在正在工作謝謝你! – Tyler 2013-05-12 16:41:58