Bonapara
Bonapara

Reputation: 369

Add sign up condition to devise

My goal is simple. I want to allow a user to sign up only if he possesses a unique secret code. I would like to check if this code is valid (being in my secret codes database) and then delete it once the user signed up.

I have a secret_code model with:

:secret_code column

I have a input for the secret code in my sign up form

<%= f.input :secret_code, required: true, autofocus: true %>

Should I personalize the class RegistrationsController < Devise::RegistrationsController?

How ? Thanks !

Upvotes: 2

Views: 682

Answers (3)

Bonapara
Bonapara

Reputation: 369

Solved! Along to @Mehmet answer I had to cutomize application_helper.rb

module ApplicationHelper
 def resource_name
   :user
 end

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

And put include ApplicationHelper after the class name in registrations_controller.rb

Upvotes: 0

John Baker
John Baker

Reputation: 2398

You can achieve this with a simple before filter. This way you don't have to mess with devise code.

   class RegistrationsController < Devise::RegistrationsController
      before_filter :check_conditions, only: :create

      def create
        super
      end

      private
        def check_conditions
          #your conditions
        end
    end

Upvotes: 0

Mehmet Adil İstikbal
Mehmet Adil İstikbal

Reputation: 600

You can do that by overriding registrationcontroller. Uncomment create method like this.

def create super end

and add a filter like:

before_action :check_secret_code, only: [:create]

and you must modify your routes to tell devise use your controller instead of its default:

devise_for :users, controllers: { registrations: "users/registrations" }

you can specify what you want in that check_secret_code method and make render 'new' if code is wrong. Hope it helps.

Upvotes: 2

Related Questions