0

I am using bcrypt-ruby for password encryption. In the documentation for the gem http://bcrypt-ruby.rubyforge.org/ they give an action to allow someone to reset their password. I am trying to implement this action.

I added the following code on my login page:

<p>Forgot your Password? <%= link_to "Click to reset your password", forgot_password_session_path, confirm: "Do you really want to reset your password?" %></p> 

Here is the code in my sessions_controller.rb file:

def forgot_password
  random_password = Array.new(10).map { (65 + rand(58)).chr }.join
  case
    when User.find_by_email(params[:session][:email_user])
      user = User.find_by_email(params[:session][:email_user])
    when User.find_by_username(params[:session][:email_user])
      user = User.find_by_username(params[:session][:email_user])
  end  
  user.password = user.password_confirmation = random_password
  user.save!
  Mailer.create_and_deliver_password_change(user, random_password)
end

Here is the definition in config/routes.db:

resources :sessions do
  member do
    get "forgot_password"
  end
end

Here is the output from rake routes:

forgot_password_session GET    /sessions/:id/forgot_password(.:format) sessions#forgot_password

When I try to click on the link on my login link I get the following error:

No route matches {:action=>"forgot_password", :controller=>"sessions"}

I do not know how it says no route matches when it appears that it does. I have restarted my rails server & spork but still get the error. I do not know the next step.

Any help will be appreciated.

Update 5/26/2012 9:14 am GMT-6:

I changed to POST, restarted my rails server & Spork. I got the same error. Here is my log from the server:

Started GET "/login" for 127.0.0.1 at 2012-05-26 09:13:36 -0500 Processing by SessionsController#new as HTML Rendered sessions/new.html.erb within layouts/application (112.4ms) Completed 500 Internal Server Error in 116ms

Here is the rake routes info:

forgot_password_session POST   /sessions/:id/forgot_password(.:format) sessions#forgot_password

1 Answers1

0

It may need to be a POST request. When I took a rails class, putting inline link calls was inconsistant. The reason you have that error is that your routes file is configured for a GET, not a POST. Just a shot in the dark, but try adding the same line but with a POST.

roguequery
  • 964
  • 13
  • 28
  • I tried again but it did not work. I updated my description with more information. Looks like more code or maybe a changed setting somewhere is needed. – Pamela Cook - LightBe Corp May 26 '12 at 14:21
  • I took another look at the rake routes statement. It looks like it is looking for a sessions/id/forgot_password path. I have the link on the login page. There is no session id at that point since the login button has not been clicked. I will move the procedure to application_controller.rb and pass the email or username to it and try again. I will post an update with my results. – Pamela Cook - LightBe Corp May 26 '12 at 14:32
  • Sorry, it didn't help... when in doubt scaffold!? i don't really know, sorry. – roguequery May 27 '12 at 06:52
  • No problem. I will revisit this at another time. I have a few more tasks for this project that I will attempt. At least I am gaining more knowledge. Thanks so much. – Pamela Cook - LightBe Corp May 28 '12 at 15:14