7

I'm experiencing some unusual behaviour with my Rails 4 Application. Every single-time I click on a link_to inside my views, my controllers actions are being called twice. For example:

In my root_url I have this standard call for users_profile:

<%= link_to('User Profile', users_profile_path, :class => "logout-button") %>

When I click this link, my console shows the following output:

Started GET "/users/profile" for 127.0.0.1 at 2013-11-25 20:45:53 -0200
Processing by Users::SessionsController#profile as HTML
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 45 ORDER BY "users"."id" ASC LIMIT 1
  InvestorProfile Load (0.5ms)  SELECT "investor_profiles".* FROM "investor_profiles" WHERE "investor_profiles"."user_id" = $1 ORDER BY "investor_profiles"."id" ASC LIMIT 1  [["user_id", 45]]
  EmployeeProfile Load (0.5ms)  SELECT "employee_profiles".* FROM "employee_profiles" WHERE "employee_profiles"."user_id" = $1 ORDER BY "employee_profiles"."id" ASC LIMIT 1  [["user_id", 45]]
  Rendered users/sessions/_investor_setup.html.erb (3.9ms)
  Rendered users/sessions/profile.html.erb within layouts/application (5.2ms)
Completed 200 OK in 19ms (Views: 11.2ms | ActiveRecord: 2.5ms)


Started GET "/users/profile" for 127.0.0.1 at 2013-11-25 20:45:53 -0200
Processing by Users::SessionsController#profile as HTML
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 45 ORDER BY "users"."id" ASC LIMIT 1
  InvestorProfile Load (0.3ms)  SELECT "investor_profiles".* FROM "investor_profiles" WHERE "investor_profiles"."user_id" = $1 ORDER BY "investor_profiles"."id" ASC LIMIT 1  [["user_id", 45]]
  EmployeeProfile Load (0.2ms)  SELECT "employee_profiles".* FROM "employee_profiles" WHERE "employee_profiles"."user_id" = $1 ORDER BY "employee_profiles"."id" ASC LIMIT 1  [["user_id", 45]]
  Rendered users/sessions/_investor_setup.html.erb (3.3ms)
  Rendered users/sessions/profile.html.erb within layouts/application (4.1ms)
Completed 200 OK in 12ms (Views: 7.5ms | ActiveRecord: 1.2ms)

People often have this behaviour when there's a remote (JS for example) calling the method, but this is not my case. the weirdest part is that, if I put the direct URL to the users_profile_path on my browser. I only get one request on my rails console:

Started GET "/users/profile" for 127.0.0.1 at 2013-11-25 20:48:17 -0200
Processing by Users::SessionsController#profile as HTML
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 45 ORDER BY "users"."id" ASC LIMIT 1
  InvestorProfile Load (0.3ms)  SELECT "investor_profiles".* FROM "investor_profiles" WHERE "investor_profiles"."user_id" = $1 ORDER BY "investor_profiles"."id" ASC LIMIT 1  [["user_id", 45]]
  EmployeeProfile Load (0.2ms)  SELECT "employee_profiles".* FROM "employee_profiles" WHERE "employee_profiles"."user_id" = $1 ORDER BY "employee_profiles"."id" ASC LIMIT 1  [["user_id", 45]]
  Rendered users/sessions/_investor_setup.html.erb (3.4ms)
  Rendered users/sessions/profile.html.erb within layouts/application (4.2ms)
Completed 200 OK in 12ms (Views: 7.7ms | ActiveRecord: 1.1ms)

I'm getting this same result for every link inside my application, not only this one.

ScieCode
  • 1,706
  • 14
  • 23
  • 1
    So there is absolutely no Javascript, are you sure? What happens in your unit test for the controller? Also try another browser see if it does the same thing. – Martin Capodici Nov 25 '13 at 23:30
  • 1
    I'm absolutely sure JS is not causing this behaviour. I tried with different browsers and all behave the same. I've been making a few changes and I believe Turbolinks gem is responsable for this. – ScieCode Nov 25 '13 at 23:33
  • Ok can you try without the gem and report back? It sounds like a client side issue of some sort though, as the web server received two requests. So Rails is doing the 'right' thing by calling your controller twice. P.S. Had a quick look and Turbolinks seems to me like it may generate javascript :-) – Martin Capodici Nov 25 '13 at 23:36
  • Can you add the controller in question? Maybe a `redirect_to` or `render` floating around? – jstim Nov 25 '13 at 23:49
  • As I predicted. Turbolinks was causing the behaviour and indeed it was a hidden JS problem within it. As soon as I removed it from my `application.js` the problem stopped. Thanks for the help guys. – ScieCode Nov 25 '13 at 23:51
  • @GuilhermeBarrosAvila Any news on how you managed to fix after removing `application.js`? – Richard Peck Nov 26 '13 at 09:03
  • @RichPeck - funny to bump into you here on Stackoverflow :-) – Martin Capodici Nov 27 '13 at 02:50
  • Ditto - will email!!! – Richard Peck Nov 27 '13 at 07:31
  • @RichPeck I just removed turbolinks gem from my `application.js`.. And I stopped receiving doubled requests. Removed this line: `//= require turbolinks` – ScieCode Nov 27 '13 at 07:34
  • @RichPeck made an answer for my question ;) – ScieCode Nov 27 '13 at 07:42
  • Thanks for the answer! Turbolinks does actually improve efficiency with the http requests your app processes - you might want to try [JQuery Turbolinks](https://github.com/kossnocorp/jquery.turbolinks) to see if that helps smooth things out! Thanks for getting back to me though – Richard Peck Nov 27 '13 at 08:59

4 Answers4

12

Rails 5/6

When I was clicking on a link, the whole controller was being called twice. I tried the accepted answer, but it does not work for me, so I just set turbolinks: false as below:

<%= link_to("Demo", @user, data: { turbolinks: false } ) %>
notapatch
  • 6,569
  • 6
  • 41
  • 45
S.Yadav
  • 4,273
  • 3
  • 37
  • 44
11

If you would like your app to still make use of Turbolinks then "Opting out of Turbolinks" on the code that is giving you problems is the way to go; just add data-no-turbolink.

I was having problems with using Bootstrap 3 and adding that fixed it. For example;

<li class="list-group-item" data-no-turbolink>
  <%= link_to download_path(item) do %>
    <button type="button" class="btn btn-success">Download</button>
  <% end %>
</li>
mrcook
  • 640
  • 6
  • 11
3

I actually managed to solve this on my own. There's a default gem that is installed with rails 4.0, it is called Turbolinks*.

For some reason, the javascript used in this gem* was causing the doubled requests on my server. That's why only GET requests were behaving like this, and POST requests were normal. I still don't fully understand why gem* causes that, but after I removed the following line from my application.js file, the doubled requests stopped.

=// require turbolinks
ScieCode
  • 1,706
  • 14
  • 23
3

Another solution is to add data-no-turbolink to the tag.

More info here: http://blog.flightswithfriends.com/post/53943440505/how-to-disable-turbolinks-in-rails-4

tmarkiewicz
  • 515
  • 5
  • 12