1

How to match license key with rake task and throw error if the key is wrong when signing up.

#sign up form    
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
      <%= devise_error_messages! %>

      <div class="field">
        <%= f.label :email %><br />
        <%= f.text_field :email, required: true  %>
      </div>

        <div class="field">
          <%= f.label :username %><br />
          <%= f.text_field :username %>
        </div>

        <div class="field">
          <%= f.label :licensekey %><br />
        </div>

        <div class="field">
          <%= f.label :contactno %><br />
          <%= f.text_field :contactno %>
        </div>

        <div class="field">
        <%= f.label :password %>
        <% if @minimum_password_length %>
        <em>(<%= @minimum_password_length %> characters minimum)</em>
        <% end %><br />
          <%= f.password_field :password, maxlength:14, required: true, pattern:'[a-zA-Z0-9]' %>
      </div>

      <div class="field">
        <%= f.label :password_confirmation %><br />
        <%= f.password_field :password_confirmation, autocomplete: "off" %>
      </div>

      <div class="actions">
        <%= f.submit "Sign up" %>
      </div>
    <% end %>

    <%= render "devise/shared/links" %>

Here is the Rake task Im trying to define

require 'rake'
task :new => :environment do
   dump_key = "P1000FFSEJZHGRDUR1CSU73A"
end
svelandiag
  • 4,231
  • 1
  • 36
  • 72

1 Answers1

0

Look you don't need to create a rake task for this.

I will suppose that licensekey is an user attribute you already defined correctly with Devise.

Then you can override Devise registration_controller.rb and perform a validation.

#registration_controller.rb
class RegistrationsController < Devise::RegistrationsController
  def create
        #HERE YOU CAN PERFORM YOUR VALIDATION
  end
end

However I would recommend perform the validation at a model level instead of controller level, also run Rake tasks within a controller is not a good practice. If you are not sure how to override the devise create action check this thread.

update

In case you still want to run your validation from a rake task, then you can run rake task within the create action of your devise controller with: %x[rake name_task] replace name_task with the name of your task.

Community
  • 1
  • 1
svelandiag
  • 4,231
  • 1
  • 36
  • 72