4

I have recently updated one of my apps to Rails 4.2. When running on my local machine I have noticed that a simple link_to to a basic show action sometimes takes a very long time to complete.

Here is the link:

= link_to team_members_path(@team) do
  Members

Here is my action (in my MembersController):

def index

  @team = Team.find_by_id(params[:team_id])

  if params[:search].present?
    @members = Member.search ThinkingSphinx::Query.escape(params[:search]), :with => {:team_id => @team.id}, :page => params[:page], :per_page => 10
  else
    @members = Member.search params[:search], :with => {:team_id => @team.id}, :page => params[:page], :per_page => 10
  end
end

I use Sphinx and ThinkingSphinx for search. When clicking the link I get an unusual long "log" in my console.

Started GET "/users/1" for ::1 at 2014-12-24 11:24:40 +0100
Processing by UsersController#show as JS
Parameters: {"id"=>"1"}
[renders stuff etc]

...

Started GET "/users/1" for ::1 at 2014-12-24 11:24:40 +0100
Processing by UsersController#show as JS
Parameters: {"id"=>"1"}
[renders stuff etc. with the same timestamp as above]

...

Started GET "/users/1" for ::1 at 2014-12-24 11:24:40 +0100
Processing by UsersController#show as JS
Parameters: {"id"=>"1"}
[renders stuff etc. with the same timestamp as above]

This action: Started GET "/users/1" seems to get called multiple times as you can see above (way more than what I pasted above), which seems to result in a pretty bad performance, even when running on my local machine.

And I'm not quite sure on how I can solve it or what's causing it. Any ideas?

Note

I actually don't know why GET "/users/1" get called at all in this case. I'm not linking to the users show action.

I have checked it using the Firefox console. See the image below:

enter image description here

Anders
  • 2,903
  • 7
  • 58
  • 114
  • 1
    Looks like turbolinks gem which was introduced with Rails 4 causing the problem as I can see to many js requests. Try to view show pay directly instead like http://localhost:3000/users/1. You can remove turbolinks if it causing the trouble which meant for performance improvement. – maximus ツ Dec 24 '14 at 13:25
  • http://stackoverflow.com/questions/18068951/rails-4-turbolinks-make-form-submit-multiple-times may help you out. – maximus ツ Dec 24 '14 at 13:34
  • Hi, thanks. I tried to disable turolinks like this: `'data-turbolinks-track' => false`, but I still have the same issue. Any other ideas? I actually don't know why `GET "/users/1"` get called at all in this case. I'm not linking to the users show action. – Anders Dec 25 '14 at 06:49
  • Please checking console log with firebug. This way you will know if your web page is actually sending the js requests. – maximus ツ Dec 26 '14 at 08:33
  • @maximus Yes, I have done it now and it seems like it's sending the requests. I have added an image from the log to my question. – Anders Dec 26 '14 at 10:36
  • @maximus don't think the view will give you any clues tbh. – Anders Dec 26 '14 at 12:11
  • it will since ajax requests are originating from view. Try to remove all the code and see if it reproduce. – maximus ツ Dec 26 '14 at 12:40
  • I think I figured it out. Seems to be a Javascript / Ajax issue. – Anders Dec 26 '14 at 13:37
  • 1
    Great, you can answer your own question, which can help others too. – maximus ツ Dec 26 '14 at 13:45
  • @maximus Yes, I have now done that. – Anders Jan 02 '15 at 12:08

1 Answers1

1

The problem seemed to be with my inline Javascript that I had in a partial form (_form.html.haml). This form is rendered on most pages since it's placed in a Bootstrap modal window and it can be presented anywhere. In this partial I perform some Javascript and Ajax calls. Each time the form is rendered I performed this action using Ajax:

:javascript
  $(document).ready(function() {

  $.ajax({
    url: "#{update_idea_team_path}",
    data: {
      team_id : $('#select_idea_team').val()
    },
    dataType: "script"
  });

For some reason this seemed to cause the problem described in my question above. Moving the Javascript and Ajax code to a .coffee.js file instead of rendering it inline solved the issue for me.

Anders
  • 2,903
  • 7
  • 58
  • 114