Rails 应用程序 Ruby 中的后退按钮跳过页面
Back Button Skipping Page in Ruby on Rails application
我发现很多一般性的帖子暗示这与重定向有关。我相信这可能是由于我设置表格的方式所致。
在 plans.html.erb
页面上,我有一个包含四个提交的表单,每个提交到同一个地方,但参数不同:
<%= form_with url: :affiliate_select_plan, class: "mx-auto" do |f| %>
<!-- Paid Plans -->
<% @plans.each_with_index do |plan, i| %>
<%= f.button 'Select Plan', value: plan[:name], type: 'submit' %>
<% end %>
<% end %>
我的 routes.rb
中有 affiliate_select_plan_path
设置:
devise_scope :affiliate do
post 'affiliate/select_plan', :to => 'affiliates/registrations#select_plan'
end
表单成功命中控制器中的 select_plan
方法,该方法将其重定向到 new_affiliate_registration_path
,并传递所需的参数。
def select_plan
redirect_to new_affiliate_registration_path(plan: plan_params[:button])
end
调用控制器中的 new
方法,将用户定向到注册页面:
# GET /resource/sign_up
def new
@plan = AffiliatePlan.find_by(nickname: params.permit(:plan)[:plan].downcase)
super
end
在此页面上,如果选择浏览器上的后退按钮,用户将返回到 plans.html.erb
之前所在的页面。
这是否与 redirect_to
有关?
编辑:
这是日志:
Started GET "/" for 127.0.0.1 at 2020-02-25 19:06:02 -0500
Processing by Affiliates::RegistrationsController#plans as HTML
Rendering affiliates/registrations/plans.html.erb within layouts/application
Rendered affiliates/registrations/plans.html.erb within layouts/application (5.2ms)
Rendered layouts/_google_analytics.html.erb (0.5ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 195ms (Views: 194.2ms | ActiveRecord: 0.0ms)
Started POST "/partner/select_plan" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#select_plan as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ck8HGRryriXleQrUjCSKjTrIRLIw273EdSu4WZnFn3kAL1mMmk7jqR1tZgnPniHsMzHFMl81vPBRuvA0/W4uSw==", "button"=>"Local"}
Unpermitted parameters: :utf8, :authenticity_token
Redirected to http://localhost:3000/partners/sign_up?plan=Local
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)
Started GET "/partners/sign_up?plan=Local" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#new as HTML
Parameters: {"plan"=>"Local"}
AffiliatePlan Load (1.2ms) SELECT "affiliate_plans".* FROM "affiliate_plans" WHERE "affiliate_plans"."nickname" = LIMIT [["nickname", "local"], ["LIMIT", 1]]
↳ app/controllers/affiliates/registrations_controller.rb:11
Rendering affiliates/registrations/new.html.erb within layouts/application
Rendered affiliates/registrations/new.html.erb within layouts/application (4.6ms)
Rendered layouts/_google_analytics.html.erb (1.1ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 191ms (Views: 187.6ms | ActiveRecord: 1.2ms)
我预感这可能与重新提交表单有关:表单和后退按钮有时有点不稳定。
但是,让我为您指出另一个方向,而不是更深入地讨论这个问题。我这样做是因为对我来说,这看起来像是有人试图找到错误问题的解决方案的经典案例。我之所以这样说,是因为根据您提供的代码和日志片段,您正在通过多个操作跳过箍来传递参数(在您的情况下是计划的名称)——如果我是对的,只是不必要的。
我会这样做:
<% @plans.each do |plan| %>
<%=
link_to 'Select Plan',
new_affiliate_registration_path(plan: plan.downcase),
class: 'some-button-class
%>
<% end %>
这样,您就不必以任何方式弄乱您的控制器。此外,由于没有 POST 请求,您不会对表单(重新)填充等问题有任何问题。
我发现很多一般性的帖子暗示这与重定向有关。我相信这可能是由于我设置表格的方式所致。
在 plans.html.erb
页面上,我有一个包含四个提交的表单,每个提交到同一个地方,但参数不同:
<%= form_with url: :affiliate_select_plan, class: "mx-auto" do |f| %>
<!-- Paid Plans -->
<% @plans.each_with_index do |plan, i| %>
<%= f.button 'Select Plan', value: plan[:name], type: 'submit' %>
<% end %>
<% end %>
我的 routes.rb
中有 affiliate_select_plan_path
设置:
devise_scope :affiliate do
post 'affiliate/select_plan', :to => 'affiliates/registrations#select_plan'
end
表单成功命中控制器中的 select_plan
方法,该方法将其重定向到 new_affiliate_registration_path
,并传递所需的参数。
def select_plan
redirect_to new_affiliate_registration_path(plan: plan_params[:button])
end
调用控制器中的 new
方法,将用户定向到注册页面:
# GET /resource/sign_up
def new
@plan = AffiliatePlan.find_by(nickname: params.permit(:plan)[:plan].downcase)
super
end
在此页面上,如果选择浏览器上的后退按钮,用户将返回到 plans.html.erb
之前所在的页面。
这是否与 redirect_to
有关?
编辑:
这是日志:
Started GET "/" for 127.0.0.1 at 2020-02-25 19:06:02 -0500
Processing by Affiliates::RegistrationsController#plans as HTML
Rendering affiliates/registrations/plans.html.erb within layouts/application
Rendered affiliates/registrations/plans.html.erb within layouts/application (5.2ms)
Rendered layouts/_google_analytics.html.erb (0.5ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 195ms (Views: 194.2ms | ActiveRecord: 0.0ms)
Started POST "/partner/select_plan" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#select_plan as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ck8HGRryriXleQrUjCSKjTrIRLIw273EdSu4WZnFn3kAL1mMmk7jqR1tZgnPniHsMzHFMl81vPBRuvA0/W4uSw==", "button"=>"Local"}
Unpermitted parameters: :utf8, :authenticity_token
Redirected to http://localhost:3000/partners/sign_up?plan=Local
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)
Started GET "/partners/sign_up?plan=Local" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#new as HTML
Parameters: {"plan"=>"Local"}
AffiliatePlan Load (1.2ms) SELECT "affiliate_plans".* FROM "affiliate_plans" WHERE "affiliate_plans"."nickname" = LIMIT [["nickname", "local"], ["LIMIT", 1]]
↳ app/controllers/affiliates/registrations_controller.rb:11
Rendering affiliates/registrations/new.html.erb within layouts/application
Rendered affiliates/registrations/new.html.erb within layouts/application (4.6ms)
Rendered layouts/_google_analytics.html.erb (1.1ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 191ms (Views: 187.6ms | ActiveRecord: 1.2ms)
我预感这可能与重新提交表单有关:表单和后退按钮有时有点不稳定。
但是,让我为您指出另一个方向,而不是更深入地讨论这个问题。我这样做是因为对我来说,这看起来像是有人试图找到错误问题的解决方案的经典案例。我之所以这样说,是因为根据您提供的代码和日志片段,您正在通过多个操作跳过箍来传递参数(在您的情况下是计划的名称)——如果我是对的,只是不必要的。
我会这样做:
<% @plans.each do |plan| %>
<%=
link_to 'Select Plan',
new_affiliate_registration_path(plan: plan.downcase),
class: 'some-button-class
%>
<% end %>
这样,您就不必以任何方式弄乱您的控制器。此外,由于没有 POST 请求,您不会对表单(重新)填充等问题有任何问题。