我的rails 4应用程序上的每个link_to都被调用两次
我正在使用我的Rails 4应用程序遇到一些不寻常的行为。 每次我点击我的视图中的link_to,我的控制器动作被调用两次。 例如:
在我的root_url
我对root_url
进行了这个标准调用:
"logout-button") %>
当我单击此链接时,我的控制台显示以下输出:
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)
当有一个远程(例如JS)调用该方法时,人们经常会有这种行为,但这不是我的情况。 最奇怪的部分是,如果我将直接URL放在浏览器上的users_profile_path
。 我只在rails控制台上收到一个请求:
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)
我为我的应用程序中的每个链接获得了相同的结果,而不仅仅是这个。
如果您希望您的应用程序仍然使用Turbolinks,那么“ 选择退出Turbolinks ”代码就会给您带来问题,这是要走的路; 只需添加data-no-turbolink
。
我在使用Bootstrap 3时遇到了问题并添加了修复它的问题。 例如;
<%= link_to download_path(item) do %> <% end %>
我实际上设法自己解决这个问题。 有一个使用rails 4.0安装的默认gem,它叫做Turbolinks * 。
出于某种原因,这个gem *中使用的javascript导致我的服务器上的加倍请求。 这就是为什么只有GET请求表现得像这样,并且POST请求是正常的。 我仍然不完全理解为什么gem *会导致这种情况,但是在我从application.js
文件中删除了以下行后,doubled请求停止了。
=// require turbolinks
另一种解决方案是向标签添加data-no-turbolink
。
更多信息: http : //blog.flightswithfriends.com/post/53943440505/how-to-disable-turbolinks-in-rails-4