简体   繁体   中英

Rails simple_form: passing params on submit

I'd like to pass custom params when user submits a form. I found this question but I still didn't get it to work.

These are my params on the form page: {"email"=>"user@example.com", "controller"=>"devise/passwords", "action"=>"new"}

Here's my form :

%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]

Was this behaviour depreciated at some point or am I doing something wrong? I'm using ruby 2.5.1 and rails 5.2.

Just declare your after password reset path so that you can pass a param :

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

This should work if your route to your successful password reset page is a collection path. If it is a member path, then just add resource like :

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

But I dont recommend using a member because it could leak data.

You describe in comments the situation when some params were passed to the controller and then happens some redirect. So, redirect do fire some new action that doesn't know what parameters was passed to prev action.

You must pass that parameters handly. For example

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

But, if you want to show the same view as before use render instead redirect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM