Charlie Egan
Charlie Egan

Reputation: 5153

NoMethodError in Devise [Registrations & Sessions] undefined method `new' for nil:NilClass

Showing ...views/devise/sessions/new.html.erb where line #3 raised:

undefined method `new' for nil:NilClass
Extracted source (around line #3):

<h2>Sign in</h2>

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>

It is exactly the same case in Registrations, and at all devise routes we had configured in fact.

It's not clear which recent change brought this on.

We've tried: *

It's always the same error:

undefined method `new' for nil:NilClass

Other questions on stackoverflow seem to be for other methods, e.g. [], new, authorize... new for nil is the only one we're getting. Any ideas much appreciated.

EDITS

user.rb (model)

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable,   :trackable, :validatable

has_many :journeys  # n.b: must pluralize here.
end

routes (user routes)

user_registration_path   POST    /users(.:format)    devise/registrations#create
PATCH    /users(.:format)    devise/registrations#update
PUT  /users(.:format)    devise/registrations#update
DELETE   /users(.:format)    devise/registrations#destroy
users_path   GET     /users(.:format)    users#index
POST     /users(.:format)    users#create

routes.rb devise_for :admins devise_for :users resources :users

get 'journeys', to: 'users#journeys'
resources :journeys

root to: "static#index"

get '/journey/new', to: 'journeys#new'    # For some reason "resources :journeys"  isn't activating the new method.

get '/admins/add_admin', to: 'admins#add_admin'

devise_scope :user do
  get 'users/sign_out', to: 'session#destroy'
  # Don't need to define sign_in here, because it works already (unlike admin).
end

devise_scope :admin do 
  get 'admins/sign_out', to: 'devise/sessions#destroy'
  get 'admins/sign_in', to: 'devise/sessions#new'
end

Upvotes: 0

Views: 1350

Answers (3)

Caleb
Caleb

Reputation: 3802

For what it's worth, here is a version of google_directions that is compatible with Devise: https://github.com/calebhaye/google-directions-ruby

Upvotes: 1

Charlie Egan
Charlie Egan

Reputation: 5153

I had added the gem: 'google_directions' recently, it appears to be that that's what caused the issue.

It requires 'extlib/hash' and for some reason this didn't play nice with devise.

Upvotes: 0

Anurag Abbott
Anurag Abbott

Reputation: 364

Try adding the following code to you application helper

  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end

Upvotes: 0

Related Questions