Search code examples
ruby-on-railsrailscastspassword-recovery

POST is going to 'new' and not 'create' Rails


So I am trying to implement the password_reset functionality into my site using bcrypt. An issue I am having is the POST is going to my new action rather to my create action.

My View

<%= form_for password_resets_path, method: 'post' do %>
<div>
  <h3>Please enter your email address</h3>
  <%= text_field_tag :email, params[:email] %>
</div>
<div>
  <%= submit_tag "Reset Password" %>
</div>

My Controller

class PasswordResetsController < ApplicationController
 def new
 end
 def create
  user = User.find_by(email: params[:email])
  user.send_password_reset if user
  redirect_to root_url, :notice => 'Email sent with password reset instructions.'
 end
end

My Routes

resources :password_resets

And I am getting this error

ActionController::RoutingError (No route matches [POST] "/password_resets/new"):

I looked at different solutions already, and since I do not have a model the @object, would not work for me. Since I am simply just trying to call to an action.

I feel like I am missing something so very simple but for the life of me I have been unable to figure it out. Many thanks in advance to whomever is the one to help me.


Solution

  • Problem: <%= form_for password_resets_path, method: 'post' do %>

    form_for needs an object. If you don't want an object, just use the form_tag helper:

    <%= form_tag password_resets_path do %>
      <%= text_field_tag :email, params[:email], placeholder: "Please enter your email address" %>
      <%= submit_tag "Reset Password" %>
    <% end %>
    

    This should work for you.