【发布时间】:2013-12-10 21:18:21
【问题描述】:
我的 Rails 4 应用程序出现一些异常行为。每次我单击视图中的 link_to 时,我的控制器操作都会被调用两次。例如:
在我的root_url 中,我对users_profile 有这个标准调用:
<%= link_to('User Profile', users_profile_path, :class => "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)
我的应用程序中的每个链接都得到了相同的结果,而不仅仅是这个。
【问题讨论】:
-
所以绝对没有 Javascript,你确定吗?在控制器的单元测试中会发生什么?也可以尝试其他浏览器看看它是否做同样的事情。
-
我绝对确定 JS 不会导致这种行为。我尝试使用不同的浏览器,但所有浏览器的行为都相同。我一直在进行一些更改,我相信 Turbolinks gem 对此负责。
-
好的,您可以尝试不使用 gem 并报告回来吗?不过,这听起来像是某种客户端问题,因为 Web 服务器收到了两个请求。所以 Rails 通过调用你的控制器两次来做“正确”的事情。附言快速浏览了一下,我觉得 Turbolinks 可能会生成 javascript :-)
-
您可以添加有问题的控制器吗?也许是
redirect_to或render漂浮在周围? -
如我所料。 Turbolinks 导致了这种行为,实际上这是一个隐藏的 JS 问题。一旦我从
application.js中删除它,问题就停止了。谢谢你们的帮助。
标签: ruby-on-rails ruby routes link-to