0

Chapter 12 is all about adding a way to reset password. In the beginning we create a controller called PasswordResets.

For the create action, the method is defined as:

def create
    @user = User.find_by(email: params[:password_reset][:email].downcase)
    if @user
      @user.create_reset_digest
      @user.send_password_reset_email
      flash[:info] = "Email sent with password reset instructions"
      redirect_to root_url
    else
      flash.now[:danger] = "Email address not found"
      render 'new'
    end
  end

My question is: how is the key [:password_reset] for params created? Was it always there and did we do something to make it present?

Params is something I find a bit confusing and can't get my head around!

doctopus
  • 5,349
  • 8
  • 53
  • 105

1 Answers1

0

Assuming that you are talking about Michael Hartl's Rails tutorial: you can see in password reset form's code there there is line like below to generate form:

<%= form_for(:password_reset, url: password_resets_path) do |f| %>

and email field is like <%= f.email_field :email, class: 'form-control' %>

so while rendering form, Rails will render forms name in format of form_name[field_name] so the email field name becomes password_reset[email]. When you access the params in controller, you get the email params value which is inside of password_reset params since params is a basic ruby hash so your code becomes params[:password_reset][:email]. If you want then you can change the form name, just change password_reset in form_for to reset or something you like and you will access it like params[:reset][:email]

I hope you understood my point. I am not good at explaining.

Sajan
  • 1,893
  • 2
  • 19
  • 39
  • So by adding `:password_reset` in : – doctopus Oct 14 '16 at 09:22
  • So by adding `:password_reset` in `<%= form_for(:password_reset, url: password_resets_path) do |f| %>` we create a params key of `[:password_reset]`? By that logic does that also mean if I wrote `<%= form_for(:xxx, url: password_resets_path) do |f| %>`, I create params key of `[:xxx]`? – doctopus Oct 14 '16 at 09:24
  • yes. since you are learning its a better way to learn by experimenting. Try changing that in your form and see what happens. If you found something odd then feel free to comment here – Sajan Oct 14 '16 at 09:26
  • When I goto the "Forgot Password" form and submit invalid email address, this is what I get: ` xxx: !ruby/object:ActionController::Parameters`. Seems to work? – doctopus Oct 14 '16 at 09:46
  • It showing that `xxx` is a ruby object of of type `ActionController::Parameters` so nothing wrong in it. where are you seeing it ? Do you mean your are not getting required functionality ? – Sajan Oct 14 '16 at 09:58
  • No I mean it is working. I see that xxx is an object of ActionController::Parameters, so it seems to be working. Thanks! – doctopus Oct 14 '16 at 09:59
  • You welcome. Keep learning and experimenting. I learned from Hartl's tutorial too. Though I had other sources – Sajan Oct 14 '16 at 10:03
  • I've been trying to learn full stack web dev from scratch for 2 months now. starting with Ruby, HTML, CSS and JS. What do you recommend next after Hartl's? – doctopus Oct 14 '16 at 10:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125689/discussion-between-sajan-and-joseph-liu). – Sajan Oct 14 '16 at 10:12