Skip to content

How To: Allow updating additional attributes when accepting invitation

Tair Assimov edited this page Feb 11, 2014 · 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 user to upload avatar or set his name. So, 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_filter :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.for(:accept_invitation) do |u|
      u.permit(:name, :password, :password_confirmation, :invitation_token, :avatar, :avatar_cache)
    end
  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