Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First argument in form cannot contain nil or be empty #2451

Closed
craigsheen opened this issue Jun 4, 2013 · 12 comments
Closed

First argument in form cannot contain nil or be empty #2451

craigsheen opened this issue Jun 4, 2013 · 12 comments

Comments

@craigsheen
Copy link

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

@craigsheen
Copy link
Author

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.

@jeremyrichardson88
Copy link

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

@craigsheen
Copy link
Author

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))

@craigsheen
Copy link
Author

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

@jeremyrichardson88
Copy link

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.

@amritdeep
Copy link

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.

@craigsheen
Copy link
Author

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.

@amritdeep
Copy link

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

@thenadz
Copy link

thenadz commented Apr 6, 2014

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).

@amritdeep
Copy link

@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.

@thenadz
Copy link

thenadz commented Apr 10, 2014

@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.

@amritdeep
Copy link

@thenadz great job dude

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants