I\'m trying to write a request test that asserts that the proper links appear on the application layout depending in whether a user is logged in or out. FWIW, I\'m using Dev
I had this error in my routes file because I had an API endpoint that I wanted to allow users to reset their passwords from, but I wanted the users to actually do the password change on the web view, which wasn't namespaced under /api
This is how I fixed the routes to make it work:
CoolApp::Application.routes.draw do
namespace :api, defaults: { format: :json } do
devise_scope :users do
post 'users/passwords', to: 'passwords#create'
end
resources :users, only: [:create, :update]
end
devise_for :users
root to: 'high_voltage/pages#show', id: 'home'
end
I had this happen to me before when I signed in as a test user on one of my apps and had made some test uploads and test posts. I then deleted that test user and when I tried to sign up again with the same test user I was displayed the message "Could not find a valid mapping for nil". What I did to solve it was to delete all of the test uploads and test post that I had made as that test user. I then tried to sign up again and it worked. By that way a faster and easier way to delete stuff is by using db browser for sqlite.
In my case this was a problem with Devis's confirm!
method. So instead of doing this (Minitest::Test
code):
setup do
@admin = create(:admin).confirm!
end
I have done this:
setup do
@admin = create(:admin)
@admin.confirm!
end
And it worked :)
Add this line to your routes.rb
# Devise routes
devise_for :users # Or the name of your custom model
Thanks to : http://blog.thefrontiergroup.com.au/2011/03/reloading-factory-girl-factories-in-the-rails-3-console/
"Devise uses a mapping between classes and routes, so when a factory built object comes through to Devise after a console reload, or a class redefinition then it will fail."
Put this in an initializer or application.rb
ActionDispatch::Callbacks.after do
# Reload the factories
return unless (Rails.env.development? || Rails.env.test?)
unless FactoryGirl.factories.blank? # first init will load factories, this should only run on subsequent reloads
FactoryGirl.factories.clear
FactoryGirl.find_definitions
end
end
Halfnelson's solution worked for me too, but since I'm using the Fabrication gem instead of FactoryGirl, I needed to make some adjustments. Here's the code I droped into a initializer:
if Rails.env.development? || Rails.env.test?
ActionDispatch::Callbacks.after do
Fabrication.clear_definitions
Rails.logger.debug 'Reloading fabricators'
end
end