Ajax "link_to" sending multiple requests controller requests when clicked really fast(Rails 4)

kpaul

Before I explain my problem, I will provide the relevant code for context. I have a Teams Model which is associated to a Scoreboard Model. The problem at hand is associated with Teams.

The View for Teams Index

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

The Partial for @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>

The link_to I'm having trouble with is for the Edit Method.

Team#edit Controller Method

  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

The edit.js.erb file

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

Now, to explain the problem.I am in the development environment. In a perfect world, I would like to click on the link_to for edit and have the specific team div disappear right away(only so I can't click on it again) and render the edit form for that Team. However, during slow loads(happens a lot), I can click on the link_to two or three times and it calls multiple controller requests to the edit method. The problem with this is that the Jquery is also executed as many times as I clicked and I end up with 2 or 3 forms for the same element.

For example, these are the log files when I click really fast on the link_to for Team with Id=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)

Each request rendered edit.js.erb so I ended up with two forms. How can I make it possible that no matter how many times I am able to click on the "link_to" before it disappears, only one edit-form for that resource is generated?

webster

You could fix this by disabling the link on first click using jQuery.

Add a class to the link

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

Disable the link when it is clicked

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related