Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise resource and resource_name helpers in custom controller

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!

like image 692
gkpo Avatar asked Dec 11 '14 16:12

gkpo


1 Answers

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
like image 144
Sachin Prasad Avatar answered Nov 19 '22 06:11

Sachin Prasad