Create Email Only

I am using Rails 4 and Devise 3.2.3. In my application, I am trying to have a registered admin user who can only create new users via email.

I followed the instructions below:

https://github.com/plataformatec/devise/wiki/How-To:-Email-only-sign-up

views> confirmations> show.html.haml

%h2 You're almost done! Now create a password to securely access your account.
= semantic_form_for(resource, :as => resource_name, :url => confirm_path) do |form|
= devise_error_messages!
= form.inputs do
= form.input :password, :input_html => {:autofocus => true}
= form.input :password_confirmation
= form.input :confirmation_token, :as => :hidden
= form.actions do
= form.action :submit, :label => 'Confirm Account

confirmations_controller

class ConfirmationsController < Devise::ConfirmationsController

  def show
    self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token]) if params[:confirmation_token].present?
    super if resource.nil? or resource.confirmed?
  end


  def confirm
    self.resource = resource_class.find_by_confirmation_token(params[resource_name][:confirmation_token]) if params[resource_name][:confirmation_token].present?
     if resource.update_attributes(params[resource_name].except(:confirmation_token).permit(:email, :password, :password_confirmation)) && resource.password_match?
      self.resource = resource_class.confirm_by_token(params[resource_name][:confirmation_token])
      set_flash_message :notice, :confirmed
      sign_in_and_redirect(resource_name, resource)
    else
      render :action => "show"
    end
  end


end

But in my show show action, I get this error:

First argument in form cannot contain nil or be empty:
= semantic_form_for(resource, :as => resource_name, :url => confirm_path) do |form|

UPDATE

The problem seems to be related to the ConfirmationsController, the show action doesn't seem to work:

The resource method does not seem to work. Trying to get it out with

class ConfirmationsController < Devise::ConfirmationsController

  def show
    self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token]) if params[:confirmation_token].present?
    # super if resource.nil? or resource.confirmed?

    render text: self.resource.nil?
  end

end

returns true

Also the confirmation_report received by me "send_confirmation_instructions" does not belong to any user instance in the database.

+4
1

, - .

Rails 4:

class ConfirmationsController < Devise::ConfirmationsController

  def show
    @original_token = params[:confirmation_token]
    digested_token = Devise.token_generator.digest(self, :confirmation_token,params[:confirmation_token])

    self.resource = resource_class.find_by_confirmation_token(digested_token) if params[:confirmation_token].present?
    super if resource.nil? or resource.confirmed?

  end


  def confirm

    digested_token = Devise.token_generator.digest(self, :confirmation_token, params[resource_name][:confirmation_token])
    self.resource = resource_class.find_by_confirmation_token(digested_token) if params[resource_name][:confirmation_token].present?
    if resource.update_attributes(params[resource_name].except(:confirmation_token).permit(:email, :password, :password_confirmation)) && resource.password_match?
      self.resource = resource_class.confirm_by_token(params[resource_name][:confirmation_token])
      set_flash_message :notice, :confirmed
      sign_in_and_redirect(resource_name, resource)
    else
      render :action => "show"
    end    

  end


end

> > show.html.haml

%h2 You're almost done! Now create a password to securely access your account.
= semantic_form_for(resource, :as => resource_name, :url => confirm_path, :method => :patch) do |form|
  = devise_error_messages!
  = form.inputs do
    = form.input :password, :input_html => {:autofocus => true}
    = form.input :password_confirmation
    = form.input :confirmation_token, :as => :hidden, :value => @original_token
  = form.actions do
    = form.action :submit, :label => 'Confirm Account'
+2

Source: https://habr.com/ru/post/1530995/


All Articles