I have an interesting situation which leaves me scratching my head.
I’ve created a controller (without a model) to generate password resets. I’ve defined a RESTful route to the controller:
routes.rb
resources :password_resets
I’ve created some action on the controller, in particular my edit action:
password_resets_controller.rb
class PasswordResetsController < ApplicationController
# code omitted
def edit
@user = User.find_by_password_reset_token!(params[:id])
end
# code omitted
end
The user can access the edit from the email that I send to the user:
password_reset.html.haml
%p To reset your password, click the URL below.
= edit_password_reset_url(@user.password_reset_token)
So far so good, I’ve managed to use the code.
Then I have the spec that tests my mailer:
describe "password reset" do
let(:user) { FactoryGirl.create(:user) }
let(:mail) { UserMailer.password_reset(user) }
it "sends user password reset url" do
mail.to.should eq([user.email])
end
end
Surprisingly, I get the following:
Failure/Error: let(:mail) { UserMailer.password_reset(user) }
ActionView::Template::Error:
No route matches {:action=>"edit", :controller=>"password_resets"}
# ./app/views/user_mailer/password_reset.html.haml:5:in `_app_views_user_mailer_password_reset_html_haml__1760284087840822602_11954840'
# ./app/mailers/user_mailer.rb:7:in `password_reset'
Yet there is a route match, at least by looking at my routes:
password_resets GET /password_resets(.:format) password_resets#index
POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset GET /password_resets/:id(.:format) password_resets#show
PUT /password_resets/:id(.:format) password_resets#update
DELETE /password_resets/:id(.:format) password_resets#destroy
What could possible go wrong in my test/routes?
Many thanks.
Check if Factory
userhaspassword_reset_token. Most likely it is nil, same routing error you can get for anyedit_something_url(nil)