Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby User Authentication with Rails Password Resets and Testing Updating a User's Forgotten Password

David Moore
David Moore
13,916 Points

undefined method `match' for nil:NilClass

I've been over my code about 20 times and I cannot locate the source of the error. I would appreciate any input!

password_resets_controller_spec.rb

            it "sets the flash[:success] message" do
                patch :update, id: user.password_reset_token, user: { password: "newpass", password_confirmation: "newpass" }
                expect(flash[:success]).to match(/Password updated/)
            end

passwords_reset_controller.rb

    def update
        @user = User.find_by(password_reset_token: params[:id])
        if @user && @user.update_attributes(user_params)
            @user.update_attribute(:password_reset_token, nil)
            session[:user_id] = @user.id
            redirect_to todo_lists_path, success: "Password updated."
        else
            flash.now[:notice] = "Password reset token not found."
            render action: 'edit'
        end
    end

Am I blind or any ideas?

David Moore
David Moore
13,916 Points

Fixed it with:

    def update
        @user = User.find_by(password_reset_token: params[:id])
        if @user && @user.update_attributes(user_params)
            @user.update_attribute(:password_reset_token, nil)
            session[:user_id] = @user.id
            flash[:success] = "Password updated"
            redirect_to todo_lists_path
        else
            flash.now[:notice] = "Password reset token not found."
            render action: 'edit'
        end
    end

But still would like to understand why the original code is not working.

2 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey David Moore you can fix this in the application_controller.rb file by adding the following:

add_flash_types :success

That will let you add a success: flash type when doing a redirect.

Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

Is your rails version before 4.0? I either really don't understand. I thought /expression/ case-sensitive by default and okay to be partly matching... (and /expression/i makes it case-insensitive)

Edit: I found some links to look up. document 3.2 and document 4.1 for redirect_to. And this post on coderwall -- I don't know exactly when but guess this change happened recently

David Moore
David Moore
13,916 Points

Thanks for the links! Yep, it is just really strange. I'm on rails 4.1.1 so it should be working just fine. I really can't figure it so I'm calling it a ghost in the system. I was able to get the same functionality with a little more code by using the standard flash[:success] but still, ya know?