我的rails 4应用程序上的每个link_to被调用两次 [英] Every link_to on my rails 4 application is being called twice

查看:47
本文介绍了我的rails 4应用程序上的每个link_to被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Rails 4应用程序遇到一些异常行为。每当我单击视图内的link_to时,我的控制器动作都会被调用两次。例如:

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:

在我的 root_url 中,我对 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)

当有远程用户(例如JS)调用方法,但这不是我的情况。最奇怪的部分是,如果我将直接URL放在浏览器上的 users_profile_path 上。我在Rails控制台上只收到一个请求:

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)

每l我得到相同的结果

推荐答案

如果您希望您的应用仍然使用Turbolink,则 <在给您带来问题的代码上href = https://github.com/rails/turbolinks/#opting-out-of-turbolinks>选择退出Turbolink 是解决之道;只需添加 data-no-turbolink

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.

我在使用Bootstrap 3时遇到了问题,并对其进行了修复。例如;

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>

这篇关于我的rails 4应用程序上的每个link_to被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆