Rails simple_form: 提交时传递参数

Rails simple_form: passing params on submit

我想在用户提交表单时传递自定义参数。我找到了这个 question 但我仍然没有让它工作。

这些是我在表单页面上的参数:{"email"=>"user@example.com", "controller"=>"devise/passwords", "action"=>"new"}

这是我的表格:

%h2 Forgot your password?
= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f|   =
f.error_notification   
.form-inputs
  = f.input :email, required: true, autofocus: true, input_html: { value: params[:email] }   
.form-actions
  = f.button :button, 'Submit', type: 'submit', name: 'email', value: params[:email]

这种行为是在某个时候贬值了还是我做错了什么? 我正在使用 ruby 2.5.1 和 rails 5.2.

您在评论中描述了将某些参数传递给控制器​​然后发生重定向的情况。因此,redirect 确实会触发一些不知道传递给上一个操作的参数的新操作。

您必须手动传递该参数。例如

redirect_to resource_path(resource, email: params[:email])

但是,如果您想显示与之前相同的视图,请使用渲染而不是重定向。

只需声明您的密码重置路径,以便您可以传递参数:

def after_sending_reset_password_instructions_path_for(resource_name) 
  my_path(email: resource.email)
end

如果您成功重设密码页面的路线是 collection 路径,这应该有效。如果它是 member 路径,则只需添加资源,如:

 def after_sending_reset_password_instructions_path_for(resource_name) 
   my_path(resource, email: resource.email)
 end

但我不建议使用 member 因为它可能会泄漏数据。