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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 我的rails 4应用程序上的每个link_to都被调用两次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用我的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]]
  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)

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

解决方法

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

猜你在找的Ruby相关文章