2011-06-14 103 views
0

我正在開發Ruby on Rails教程。我有異步按鈕工作。有趣的是,當我點擊按鈕時,每個javascript調用都會連續調用兩次。關於我如何發送的任何想法只發送一個請求?這是我的日誌JS請求連續調用兩次

Started POST "/tag_user_relationships/123" for 
127.0.0.1 at 2011-06-13 21:18:59 -0700 Processing by TagUserRelationshipsController#destroy as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"goedvibRxKtDRiAufp1ThWJP0rRBU2cMH2xp7qodKws=", "commit"=>"Unfollow", "id"=>"123"} User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 TagUserRelationship Load (0.2ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 LIMIT 1 Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`id` 
= 9 LIMIT 1 TagUserRelationship Load (0.3ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`tag_id` = 9 AND (`tag_user_relationships`.user_id 
= 2) LIMIT 1 SQL (0.1ms) BEGIN AREL (0.2ms) DELETE FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 SQL (0.4ms) COMMIT SQL (0.4ms) SELECT COUNT(*) FROM `users` INNER JOIN `tag_user_relationships` ON `users`.id = `tag_user_relationships`.user_id WHERE ((`tag_user_relationships`.tag_id = 9)) SQL (0.3ms) SELECT COUNT(*) FROM `tags` INNER JOIN `tag_user_relationships` ON `tags`.id 
= `tag_user_relationships`.tag_id WHERE ((`tag_user_relationships`.user_id = 2)) Rendered tag_user_relationships/_form.js.erb (15.8ms) Rendered tags/_follow.html.erb (2.1ms) Rendered tag_user_relationships/destroy.js.erb (20.3ms) Completed 200 OK in 138ms (Views: 28.1ms | ActiveRecord: 2.3ms) 


Started POST "/tag_user_relationships/123" for 
127.0.0.1 at 2011-06-13 21:18:59 -0700 Processing by TagUserRelationshipsController#destroy as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"goedvibRxKtDRiAufp1ThWJP0rRBU2cMH2xp7qodKws=", "id"=>"123"} User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 TagUserRelationship Load (0.2ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 LIMIT 1 Completed 404 Not Found in 70ms 

ActiveRecord::RecordNotFound (Couldn't find TagUserRelationship with ID=123): app/controllers/tag_user_relationships_controller.rb:14:in `destroy' 

Rendered /Users/me/.rvm/gems/[email protected]/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms) Rendered /Users/me/.rvm/gems/[email protected]/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (153.6ms) Rendered /Users/me/.rvm/gems/[email protected]/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (160.2ms) 

這裏是我的看法

$("#<%= "follower_info#{@tag.id}" %>").html("<%=escape_javascript(pluralize(@tag.followers.count,'follower'))%>");  
var link = $('<a>').attr('href',"<%=user_tags_path(current_user) %>").text("<%= escape_javascript(pluralize(current_user.beats.count,'tag')) %>"); 
$("#<%= "user#{current_user.id}_following" %>").html(link); $("#<%= "follow_form#{@tag.id}" %>").html("<%= escape_javascript("#{render('tags/unfollow', :tag => @tag)}").html_safe %>"); 

我的控制器

def create 
    @tag = Tag.find(params[:tag_user_relationship][:tag_id]) 
    current_user.follow_tag!(@tag) 
    respond_to do |format| 
     format.html { redirect_to @tag } 
     format.js 
    end 
    end 

回答

4

最近我有同樣的問題。有沒有機會安裝ajax gem,並且還將jquery代碼包含在Javascript文件夾中?如果是這樣,通話將會進行兩次。

+0

我不認爲我安裝了ajax寶石。你把它添加到你的gemfile嗎?我絕對有jQuery代碼。 – Cyrus 2011-06-14 23:56:05

+0

有沒有機會將jquery代碼包含在javascripys文件夾中兩次? – cmwright 2011-06-15 19:49:47

+0

賓果 - 我有jquery.min和jquery_ujs – Cyrus 2011-06-15 23:27:03