Question

I'm new to RoR and have been working my way through the Hartl tutorial (which has been great). I've followed up through Chapter 9 successfully (tweaking things a bit since my ultimate goal is not to make a microposts site). At that point, I decided that I would like to add a 'remember me' check box and reset password functionality to my app, so I bounced over to the railscast tutorial (as suggested by Hartl). The check box went very smoothly, but I've hit a brick wall with the password reset section. It's been one error after the next. I have to admit that I couldn't help myself and tweak a little - I tried to use theform_for syntax instead of the form_tag syntax. I've gotten as far as being able to submit an email address, but then I get a No route matches [POST] "/reset_password/new" message. I've spent the last two days reading similar posts on stackoverflow and trying out the suggestions, but I just can't seem to come up with something that works. Please help!

Here's the nitty gritty:

My password reset view is located at /app/views/reset_password/new.html.erb:

<% provide(:title, 'Reset Password') %>
<h1>Reset Password</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for @user, url: new_reset_password_path do |f| %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.submit "Reset Password", class: "btn btn-large btn-methyl" %>
    <% end %>
  </div>
</div>

My controller is located at /app/controllers/reset_password_controller.rb:

class ResetPasswordController < ApplicationController

  def new
    @user = User.new
  end

  def show
  end

  def create
    @user = User.find_by_email(params[:email].downcase)
    user.send_password_reset if user
    redirect_to root_path, notice: "Email sent with password reset instructions."
  end

  def edit
    @user = User.find_by_password_reset_token!(params[:id])
  end

  def update
    @user = User.find_by_password_reset_token!(params[:id])
    if @user.reset_password_sent_at < 2.hours.ago
      redirect_to_new password_reset_path, alert: "Reset password request has expired."
    elsif @user.update_attributes(params[:user])
      redirect_to root_path, notice: "Password has been reset!"
    else
      render :edit
    end
  end
end

My routes are located at /config/routes.rb:

Methylme::Application.routes.draw do

  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :reset_password

  root to: 'static_pages#home'

  match '/signup', to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/help', to: 'static_pages#help'
  match '/about', to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
.
.
.
end

Finally, $ rake routes reports the following:

               users GET    /users(.:format)                   users#index
                     POST   /users(.:format)                   users#create
            new_user GET    /users/new(.:format)               users#new
           edit_user GET    /users/:id/edit(.:format)          users#edit
                user GET    /users/:id(.:format)               users#show
                     PUT    /users/:id(.:format)               users#update
                     DELETE /users/:id(.:format)               users#destroy
            sessions POST   /sessions(.:format)                sessions#create
         new_session GET    /sessions/new(.:format)            sessions#new
             session DELETE /sessions/:id(.:format)            sessions#destroy
reset_password_index GET    /reset_password(.:format)          reset_password#index
                     POST   /reset_password(.:format)          reset_password#create
  new_reset_password GET    /reset_password/new(.:format)      reset_password#new
 edit_reset_password GET    /reset_password/:id/edit(.:format) reset_password#edit
      reset_password GET    /reset_password/:id(.:format)      reset_password#show
                     PUT    /reset_password/:id(.:format)      reset_password#update
                     DELETE /reset_password/:id(.:format)      reset_password#destroy
                root        /                                  static_pages#home
              signup        /signup(.:format)                  users#new
              signin        /signin(.:format)                  sessions#new
             signout DELETE /signout(.:format)                 sessions#destroy
                help        /help(.:format)                    static_pages#help
               about        /about(.:format)                   static_pages#about
             contact        /contact(.:format)                 static_pages#contact

Thanks in advance for your help!

Was it helpful?

Solution

I don't think you want to link to new_reset_password_path (new) in your password reset view, but to reset_password_path (create), which does send the reset password email.

If your routes don't do what you expect (for instance, the create route does not have an associated xxx_path name) you should simply declare them individually, with

post '/reset_password', to: 'reset_password#create', as: 'send_reset_password' # for example
...

OTHER TIPS

This is one of the best authentication tutorial by Ryan,

http://railscasts.com/episodes/250-authentication-from-scratch-revised

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top