Ruby-on-rails – Devise resource and resource_name helpers in custom controller

devisehelpersruby-on-rails

I extended the default Devise RegistrationsController using routes:

# use my own controller for devise registrations
devise_for :users, :controllers => { registrations: 'registrations' }

In this controller, I have a method called new_from_invitation. This method renders a custom signup form to display in case the user comes from an invitation.

I copied the file devise/registrations/new.html.erb. So my custom signup form looks like this:

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

This throws an ArgumentError because resource is nil. Same as resource name.
Somehow, the resource and resource_name helpers are available in the default devise/registrations/new method but I can't figure out how. And therefore can't figure out a way to have these helpers available for my custom method.
Any help would be very much appreciated!

Best Answer

Add this to your Application helper file :

  def resource_name
    :user
  end

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