我的rails 4应用程序上的每个link_to都被调用两次

Sci*_*ode 6 ruby routes ruby-on-rails link-to

我正在使用我的Rails 4应用程序遇到一些不寻常的行为.每次我点击视图中的link_to,我的控制器动作都会被调用两次.例如:

在我的root_url标准呼吁users_profile:

<%= link_to('User Profile', users_profile_path, :class => "logout-button") %>
Run Code Online (Sandbox Code Playgroud)

当我单击此链接时,我的控制台显示以下输出:

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)
Run Code Online (Sandbox Code Playgroud)

当有一个远程(例如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)
Run Code Online (Sandbox Code Playgroud)

对于我的应用程序中的每个链接,我得到了相同的结果,而不仅仅是这个.

Mic*_*ook 10

如果您希望自己的应用程序仍然使用Turbolinks,那么" 选择不再使用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>
Run Code Online (Sandbox Code Playgroud)


S.Y*_*dav 9

导轨 5/6

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

<%= link_to("Demo", @user, data: { turbolinks: false } ) %>
Run Code Online (Sandbox Code Playgroud)

  • 对于 Rails 6,此解决方案有效(与已接受的答案相反)。 (2认同)