Kiran
Kiran

Reputation: 5526

rails mailer generating invalid URL

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: "[email protected]"
  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

Upvotes: 0

Views: 186

Answers (1)

willglynn
willglynn

Reputation: 11520

This seems like config/routes.rb contains resource :password_resets instead of resources :password_resets. As a singular resource, this would add an edit_password_resets route to /password_resets/edit(.:format). Passing a value into edit_password_resets_url would map to edit.<value>, consistent with the symptom you describe.

Changing this to resources :password_resets should fix the issue. It will also rename the route to edit_password_reset -- singular because it applies to a member, rather than plural which would apply to the collection.

Upvotes: 1

Related Questions