【问题标题】:Every link_to on my rails 4 application is being called twice我的 rails 4 应用程序上的每个 link_to 都被调用了两次
【发布时间】: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_torender 漂浮在周围?
  • 如我所料。 Turbolinks 导致了这种行为,实际上这是一个隐藏的 JS 问题。一旦我从application.js 中删除它,问题就停止了。谢谢你们的帮助。

标签: ruby-on-rails ruby routes link-to


【解决方案1】:

实际上,我自己设法解决了这个问题。 rails 4.0 安装了一个默认的 gem,它被称为Turbolinks*

由于某种原因,此 gem* 中使用的 javascript 导致我的服务器上的请求加倍。这就是为什么只有 GET 请求的行为是这样的,而 POST 请求是正常的。 我仍然不完全明白为什么 gem* 会导致这种情况,但是在我从 application.js 文件中删除以下行之后,加倍的请求停止了。

=// require turbolinks

【讨论】:

    【解决方案2】:

    如果您希望您的应用程序仍然使用 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>
    

    【讨论】:

      【解决方案3】:

      另一种解决方案是在标签中添加data-no-turbolink

      更多信息在这里:http://blog.flightswithfriends.com/post/53943440505/how-to-disable-turbolinks-in-rails-4

      【讨论】:

        【解决方案4】:

        轨道 5/6

        当我点击一个链接时,整个控制器被调用了两次。我尝试了接受的答案,但它对我不起作用,所以我将turbolinks: false 设置为如下:

        <%= link_to("Demo", @user, data: { turbolinks: false } ) %>
        

        【讨论】:

        • 对于 Rails 6,此解决方案有效(与接受的答案相反)。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多