0

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.

Joanna Gaudyn
  • 597
  • 3
  • 21
  • `simple_form_for(resource, as: resource_name, url: password_path(resource_name, email: params[:email]), html: {...` – Vasilisa May 29 '18 at 10:40
  • @Vasilisa this doesn't seem to change anything: I still have the param available on the form page but not after submit. – Joanna Gaudyn May 29 '18 at 10:55
  • Did you remove name and value from button? – Vasilisa May 29 '18 at 11:02
  • @Vasilisa I didn't before (wasn't really clear from your comment) but tried it now - no change. – Joanna Gaudyn May 29 '18 at 11:05
  • Sorry, just noticed - you already have input for email. Why do you want to pass it one more time? And what do you have now in params after submitting form? – Vasilisa May 29 '18 at 11:11
  • just add a hidden field to your form, you can retrieve this param from the form params in the controller. – Maxence May 29 '18 at 11:21
  • @Vasilisa I don't want to pass it one more time, I'd just like to keep it in params after redirection. Right now after submit my params are `{"controller"=>"devise/sessions", "action"=>"new"}` – Joanna Gaudyn May 29 '18 at 11:26
  • @Maxence I tried with a hidden field but that also doesn't let me pass the param to the page after redirection on submit. – Joanna Gaudyn May 29 '18 at 11:27
  • Well this is the purpose of instance variable to pass data from controller to the view...? – Maxence May 29 '18 at 11:34
  • `{"controller"=>"devise/sessions", "action"=>"new"}` isn't equal `password_path(resource_name)`, I suppose. It should be devise/passwords, action create. Something goes totally wrong. Do you have any authorization restrictions? – Vasilisa May 29 '18 at 12:23
  • 1
    You are in `devise/sessions/new` when you click "Forgot password" and get taken to `devise/passwords/new`, and the original idea was to redirect back to login after an email is submitted (so you'd be back at `sessions/new`), with the email already filled in from params. – Joanna Gaudyn May 29 '18 at 12:27
  • Your form goes to devise/passwords/create, which redirects you to devise/session/new. So you need to change Devise::PasswordsController, not the form – Vasilisa May 29 '18 at 12:53

2 Answers2

1

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.

Maxence
  • 2,029
  • 4
  • 18
  • 37
0

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.

Leo
  • 1,673
  • 1
  • 13
  • 15
  • I'm using devise passwords_controller and the `after_sending_reset_password_instructions_path_for` method. I'd like to redirect to a static page saying something like "Password reset instructions were sent to #{params[:email]}". I tried with `def after_sending_reset_password_instructions_path_for(resource_name) password_reset_path(email: params[:email]) end` but that also doesn't seem to pass the param. – Joanna Gaudyn May 29 '18 at 12:16
  • Devise? @Maxence already given the correct answer how to do that for Devise. – Leo May 29 '18 at 12:46
  • 1
    Also if you want not display email in URL, you can save email in the user session and then put that into view as variable @email = session['email'] – Leo May 29 '18 at 12:50