Question

I have a custom registration in devise, but Its not doing what I'm supposed to.

I'm trying to create new data:

  def create
    build_resource(sign_up_params)

    if resource.save

      yield resource if block_given?
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
        if params[:user][:role] == "Proof"
          Proof.create(:user_id => current_user.id, :first_name => params[:user][:first_name], :last_name => params[:user][:last_name])
        end
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

What's custom is this part:

    if params[:user][:role] == "Proof"
      Proof.create(:user_id => current_user.id, :first_name => params[:user][:first_name], :last_name => params[:user][:last_name])
    end

But for some reason, its not doing what its supposed to. It won't create the new data in my database upon signup. Is this the wrong location where this is supposed to go?

  Parameters: {"utf8"=>"✓",
              "authenticity_token"=>"3QTVW3x3cnWho5yd6aEMC31iDIDu+IHQClpQao9VBqc=", 
              "user"=>{"first_name"=>"First", 
                       "last_name"=>"Last", 
                       "role"=>"Proof"}}
Was it helpful?

Solution

In create action, swap

   respond_with resource, :location => after_sign_up_path_for(resource)
    if params[:user][:role] == "Proof"
      Proof.create(:user_id => current_user.id, :first_name => params[:user][:first_name], :last_name => params[:user][:last_name])
    end

To

    if params[:user][:role] == "Proof"
      Proof.create(:user_id => current_user.id, :first_name => params[:user][:first_name], :last_name => params[:user][:last_name])
    end
   respond_with resource, :location => after_sign_up_path_for(resource)

Currently, Proof record is not getting created as the code from if statement is dead code and is never executed. The method create returns when you call respond_with method. Executing the if statement before respond_with call should resolve your issue.

UPDATE

As per the chat session with OP, the Proof record was not getting created because of a validation on custom_url field. Suggested to change the validation with on: :update option.

validates :custom_url, uniqueness: true, on: :update
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top