Devise: First argument in form cannot contain nil or be empty

Created on 4 Jun 2013  路  12Comments  路  Source: heartcombo/devise

When I start up my application and it takes me to the sign in page, I am getting the error:

First argument in form cannot contain nil or be empty

which is corresponding to this line:

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>

I tried searching and couldn't seem to find any issues regarding this. Any ideas on a fix?

I am running rails 4 with devise 3

Most helpful comment

Or i've just realised my stupidity, instead of the above you can simply add "super" inside your new method, which would then inherit from the devise controller new method.

def new
  super
end

All 12 comments

Ah nevermind, it turns out this was due to not having code in the custom controller new method. This worked pre rails 4, but there has since been code added which catches this and shows an error.

I am having the same issue. Just wondering how you fixed this. thanks

Basically the error is due to a change in Rails which means you can no longer pass blank variables into a form_for.

You need to therefore make sure you add the resource code as below, to your custom controller.

    self.resource = resource_class.new(sign_in_params)
    clean_up_passwords(resource)
    respond_with(resource, serialize_options(resource))

Or i've just realised my stupidity, instead of the above you can simply add "super" inside your new method, which would then inherit from the devise controller new method.

def new
  super
end

Not sure why but the only diff from the old devise controller and the new one was that first line
old: self.resource = build_resource(nil, :unsafe => true)
new: self.resource = resource_class.new(sign_in_params)
so not sure why its necessary to call super when I hadn't before but thanks that was it.

In my custom controller, I have

def new
super
end

I have following code in application helper
## Devise helper method for resource
def resource_name
:user
end

def resource
@resource ||= User.new
end

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

But none of this are working. Still facing same problem.

I didn't have any of that code in the application helper as, although I am not 100%, that should probably be included anyway.. if you have a customer controller you should just need;

def new
  super
end

which worked for me.

It doesn't work for me. But when I include ApplicationHelper module in customer controller then it work. I have define devise helper method for resource in ApplicationHelper. So, I include it in my customer controller. For example.

application_helper.rb

module ApplicationHelper

## Devise helper method for resource
def resource_name
:user
end

def resource
@resource ||= User.new
end

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

...................
end

password_controller.rb

class PasswordsController < Devise::PasswordsController
include ApplicationHelper
..............
end

Using Rails 4, I'm having this same issue without a custom controller... I only have custom views (which are just the standard ones generated through devise).

@thenadz In view (I don't in what sense you are using custom views ), if you are using custom views the passed parameter. Don't use devise @resouce method in that case.

@amritdeep I was using the default views generated by rails generate devise:views (without any modifications). If this creates invalid syntax then surely that is a gem bug that should be resolved. It's also worth noting that using the default scoped view (created using: rails generate devise:views users) did not have the same issue and worked as expected.

@thenadz great job dude

Was this page helpful?
0 / 5 - 0 ratings