2016-04-30 51 views
2

在我解釋我的問題之前,我將提供相關代碼的上下文。我有一個與記分牌模型相關的球隊模型。手頭的問題與團隊有關。Ajax「link_to」發送多個請求控制器請求時,點擊的速度非常快(Rails 4)

視角下的團隊指數

<div class="team-list"> 
    <%= render @scoreboard.teams.reject(&:new_record?) %> 
</div> 

的部分爲@ scoreboard.teams

<div class="row team-div" id="team_<%=team.id%>"> 
    <div class="col-xs-3 team-div-1"> <%= team.name %> </div> 

    <%= link_to (edit_scoreboard_team_path(@scoreboard, team)), remote: true do %> 
     <div class="col-xs-6 team-data"> 
     <div class="row"> 
      <div class="col-xs-4 team-div-2"><%= team.win %> </div> 
      <div class="col-xs-4 team-div-2"><%= team.loss %> </div> 
      <div class="col-xs-4 team-div-2"><%= team.tie %></div> 
     </div> 
     </div> 
    <% end %> 

    <div class="col-xs-1 team-div-2"> 
     <%= team.win + team.loss + team.tie %> 
    </div> 

    <div class="col-xs-1 team-div-2"> 
     <% total_games = team.win.to_i + team.loss.to_i + team.tie.to_i %> 
    <% if total_games == 0 %> 
    <%= "N/A" %> 
    <% else %> 
    <%= (team.win.to_i/(total_games).to_f*100).round(2) %><%="%"%> 
    <% end %> 
    </div> 

    <div class="col-xs-1 team-setting"> 
      <i class="fa fa-trash-o fa-lg" aria-hidden="true"></i> 
    </div> 

</div> 

我遇到麻煩的link_to是爲編輯方法

隊#編輯控制器的方法

def edit 
    @scoreboard = Scoreboard.find(params[:scoreboard_id]) 
    @team = @scoreboard.teams.find(params[:id]) 
    respond_to do |format| 
    format.html {redirect_to scoreboard_url(@team.scoreboard_id)} 
    format.js 
    end 
    end 

的edit.js.erb文件

$("#team_<%[email protected]%>").hide(); 
$("#team_<%[email protected]%>").before("<%= j render 'teamedit'%>"); 

現在,解釋疑難問題。我在開發環境中。在完美的世界中,我想點擊link_to進行編輯,並讓特定的團隊div馬上消失(只有這樣我才能再次點擊它)並呈現該團隊的編輯表單。然而,在緩慢加載期間(發生很多),我可以點擊link_to兩到三次,並調用編輯方法的多個控制器請求。這個問題是,Jquery的執行次數也是我點擊的次數最多,同樣的元素是2或3個表單。

例如,這些日誌文件,當我點擊真快上ID爲的link_to的團隊= 50:

Started GET "/scoreboards/1/teams/50/edit" for 99.239.140.167 at 2016-04-30 08:07:52 +0000 
Processing by TeamsController#edit as JS 
    Parameters: {"scoreboard_id"=>"1", "id"=>"50"} 
    User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] 
    Scoreboard Load (0.3ms) SELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = $1 ORDER BY "score oards"."created_at" DESC LIMIT 1 [["id", 1]] 
    Team Load (0.3ms) SELECT "teams".* FROM "teams" WHERE "teams"."scoreboard_id" = $1 AND "teams"."id" = $2 LIMIT 1 [["scoreboard_id", 1], ["id", 50]] 
    Rendered teams/_teamedit.html.erb (3.1ms) 
    Rendered teams/edit.js.erb (7.1ms) 
Completed 200 OK in 35ms (Views: 27.6ms | ActiveRecord: 1.2ms) 


Started GET "/scoreboards/1/teams/50/edit" for 99.239.140.167 at 2016-04-30 08:07:52 +0000 
Processing by TeamsController#edit as JS 
    Parameters: {"scoreboard_id"=>"1", "id"=>"50"} 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] 
    Scoreboard Load (0.4ms) SELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = $1 ORDER BY "scoreboards"."created_at" DESC LIMIT 1 [["id", 1]] 
    Team Load (0.3ms) SELECT "teams".* FROM "teams" WHERE "teams"."scoreboard_id" = $1 AND "teams"."id" = $2 LIMIT 1 [["scoreboard_id", 1], ["id", 50]] 
    Rendered teams/_teamedit.html.erb (2.1ms) 
    Rendered teams/edit.js.erb (5.6ms) 
Completed 200 OK in 30ms (Views: 24.2ms | ActiveRecord: 1.0ms) 

呈現的每個請求edit.js.erb所以我結束了兩個形式。 我怎樣才能讓它成爲可能:無論我在點擊「link_to」之前點擊了多少次,它只會生成一個該資源的編輯表單?

回答

2

您可以通過使用jQuery禁用首次點擊鏈接來解決此問題。

添加類的鏈接

<%= link_to (edit_scoreboard_team_path(@scoreboard, team)), remote: true, class: "team-edit-link" do %> 
    ... 
<% end %> 

禁用時,它被點擊

$('.team-edit-link').click(function(e) { 
    if($(this).hasClass('disabled')){ 
    e.preventDefault(); 
    } 
    else{ 
    $(this).addClass('disabled'); 
    } 
}); 
+0

嗨鏈接!感謝您的回答。我會將該代碼放入edit.js.erb文件或application.js中嗎? – kpaul

+0

您可以將其添加到application.js或特定於該頁面/控制器的任何自定義js文件中。確保您將其綁定在文檔就緒功能中。 – webster

+0

嘿。謝謝你的幫助。我對自己的代碼做了一些自己的修改,但它絕對是正確的想法。 – kpaul