Skip to content

How To: Allow updating additional attributes when accepting invitation

RowanMB edited this page Dec 15, 2018 · 4 revisions

Sometimes you want to update other resource attributes in addition to password and password confirmation during invitation acceptance process. For instance, you would like to allow the user to upload avatar or set their name. Here is how to do it:

Override default controller in your routes:

# config/routes.rb
devise_for :users, controllers: {
  invitations: "invitations"
}

Create controller with the following code. Set the parameters you want to update in update_sanitized_params:

class InvitationsController < Devise::InvitationsController

  before_action :update_sanitized_params, only: :update

  # PUT /resource/invitation
  def update
    respond_to do |format|
      format.js do
        invitation_token = Devise.token_generator.digest(resource_class, :invitation_token, update_resource_params[:invitation_token])
        self.resource = resource_class.where(invitation_token: invitation_token).first
        resource.skip_password = true
        resource.update_attributes update_resource_params.except(:invitation_token)
      end
      format.html do
        super
      end
    end
  end


  protected

  def update_sanitized_params
    devise_parameter_sanitizer.permit(:accept_invitation, keys: [:name, :password, :password_confirmation, :invitation_token, :avatar, :avatar_cache])
  end
end

Finally, generate views and customize your app/views/invitations/edit template. You would need to send requests with JavaScript not to conflict with devise_invitable default procedures.

rails g devise_invitable:views