I am trying to add password reset functionality to my rails app using action mailer. Everything seems to be working fine except that the generated link to reset the password is incorrect.
Here are the files:
File user_mailer.rb:
class UserMailer < ActionMailer::Base
default from: "from@example.com"
def password_reset(user)
@user = user #make the user available in mailer template
mail :to => user.email, :subject => "Password Reset"
end
end
File password_reset.text.erb contains the following link:
<%= edit_password_resets_url(@user.password_reset_token) %>
I can see the user variable is passed properly as I get the email and token values. However, the URL generated in the mailer looks like the following:
http://localhost:3000/password_reset/edit.jo_jYhkjsdjskjdskYHJSDA
However, the expected value is like
http://localhost:3000/password_resets/jo_jYhkjsdjskjdskYHJSDA/edit
Have the following in routes.rb
resource :password_resets
Also, rake routes shows the following:
edit_password_resets GET /password_resets(.:format) password_resets#edit
What could be going wrong?
Note: I am following Ryan bates’ rails casts #274
This seems like
config/routes.rbcontainsresource :password_resetsinstead ofresources :password_resets. As a singular resource, this would add anedit_password_resetsroute to/password_resets/edit(.:format). Passing a value intoedit_password_resets_urlwould map toedit.<value>, consistent with the symptom you describe.Changing this to
resources :password_resetsshould fix the issue. It will also rename the route toedit_password_reset— singular because it applies to a member, rather than plural which would apply to the collection.