我发现许多通用的帖子暗示这与重定向有关。我认为这可能是由于我如何设置表格所致。
在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 %>
我affiliate_select_plan_path
在我的设置routes.rb
:
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" = $1 LIMIT $2 [["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请求,因此表单(重新)填充等问题不会出现任何问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句