I have a working registration and login using Devise and Rails 4. However, I also created an invitation system in which existing users can invite others to join their organization. For this reason, I need a new page that contains both the registration and registration form, so when they click on their inviting link (which looks like / register_from_invitation / TOKEN_HERE), they will be able to register to accept the invitation, or if they already have an account, log in to accept.
So, on my routes, I:
devise_for :users, :controllers => {
:sessions => "sessions",
:registrations => "registrations",
:confirmations => "confirmations",
:passwords => "passwords"
}
devise_scope :user do
get "register_from_invitation/:token", to: "registrations#new_from_invitation", as: "register_from_invitation"
post "register_from_invitation/:token", to: "registrations#create_from_invitation", as: "create_registration_from_invitation"
post "sign_in_from_invitation/:token", to: "sessions#create_from_invitation", as: "sign_in_from_invitation"
end
. "" new create, ( ) :
def new_from_invitation
@invitation = Invitation.where(token: params[:token]).first
build_resource({})
self.resource.organisations << @invitation.organisation
respond_with self.resource
end
def create_from_invitation
@invitation = Invitation.where(token: params[:token]).first
build_resource(sign_up_params)
if resource.save
resource.organisations << @invitation.organisation
resource.roles[0].roles << @invitation.role.to_sym
resource.roles[0].save
@invitation.destroy
SignupNotificationMailer.signup_notification_email(resource).deliver
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 => '/users/sign_up'
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, :location => '/users/sign_up'
end
else
clean_up_passwords resource
respond_with resource
end
end
, , URL (/register_from_invitation/TOKEN_HERE), new, respond_with resource :
respond_with resource, action: new_from_invitation
... , , , : Render and/or redirect were called multiple times in this action.
* _from_invitation? sign_in_from_invitation .
.