我的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)
我的应用程序中的每个链接都得到了相同的结果,而不仅仅是这个。
答案 0 :(得分:10)
如果您希望您的应用仍然使用Turbolinks,那么“Opting out of Turbolinks”代码会给您带来问题的方法;只需添加data-no-turbolink
。
我在使用Bootstrap 3时遇到了问题并添加了修复它的问题。例如;
<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>
答案 1 :(得分:3)
我实际上设法自己解决这个问题。 有一个使用rails 4.0安装的默认gem,它被称为Turbolinks*。
出于某种原因,这个gem *中使用的javascript导致我的服务器上的加倍请求。这就是为什么只有GET请求表现得像这样,并且POST请求是正常的。
我仍然不完全理解为什么gem *会导致这种情况,但是在我从application.js
文件中删除了以下行后,双重请求停止了。
=// require turbolinks
答案 2 :(得分:3)
另一种解决方案是将data-no-turbolink
添加到代码中。
此处有更多信息:http://blog.flightswithfriends.com/post/53943440505/how-to-disable-turbolinks-in-rails-4
答案 3 :(得分:0)
我在 Rails 5 中也遇到了相同的问题,这是我的机器详细信息-
$ rails -v
Rails 5.2.2
$ ruby -v
ruby 2.6.1p33
每当我单击链接时,整个控制器就会被调用两次。
我尝试了接受的答案,但对我无效,
所以我只将turbolinks: false
设置为以下-
Link is: <%= link_to("Demo link", "/abc/pqr/#{current_user.id}", data: { turbolinks: false } ) %>
这对我有用,谢谢上面的提示!