Closed thibaudclement closed 1 year ago
I think the good way is your first attempt, adding conditions to validations. There are some methods on invitable: accepting_invitation?, invitation_accepted?, accepted_or_not_invited?.
I can think 2 ways, although I haven't tested:
if: [:confirmed?, :accepted_or_not_invited?]
unless: :accepting_invitation?
so it will skip those validations while invitation is being accepted.@scambra thank you very much for your response : I did not even have to try your second suggestion, because the first worked right away, like a charm.
So, if it's any helpful for anyone else, to solve this issue, in the User
model, I replaced:
validates :name, presence: true, if: :confirmed?
validates :role, presence: true, if: :confirmed?
with:
validates :name, presence: true, if: [:confirmed?, :accepted_or_not_invited?]
validates :role, presence: true, if: [:confirmed?, :accepted_or_not_invited?]
I am building a Rails 7 application, with Devise to manage authentication, and Devise Invitable to manage invitations (on the
User
model).When a user receives an invite and clicks on the invitation link, they are redirected to the
invitations/edit
view, where they are able to sign up, by creating apassword
(theiremail
address is auto-populated in theemail
field).Because I have implemented a multi-step signup process, users are not required to provide a
name
and select arole
until after they have confirmed their email address (i.e.confirmed?
istrue
), or in the case of users who sign up from an invite, after they have accepted their invite (which confirms their account automatically behind the scenes).Therefore, in the
User
model, I have the following validation:This works as expected for users who sign up on their own (i.e. users who do not sign up from an invite), since Devise's
registrations/new
(i.e. thecreate
action of theRegistrationsController
) successfully ignores validation on the:name
and:role
attributes until the user isconfirmed?
.However, this does not work as expected for users who sign up from an invite, since Devise Invitable's
invitations/edit
(i.e. theupdate
action of theInvitationsController
) seem to enforce the validation on the:name
and:role
of the user even when the user is notconfirmed?
, which results in the following error:As a consequence, the form in
invitations/edit
is served again to the user, with thepassword
field cleared, and the user is not updated, and the user has no way of providing aname
or arole
, since those are (again) not required until after they have confirmed their email address (or accepted an invitation, when signing up from an invite, which confirms their email address behind the scenes).Here is what I have tried to avoid this issue so far, to no avail:
invitation_token
,invitation_accepted_at
,persisted
, etc.) to try and ignore those validations, but no matter what I do, it seems that the validations are triggered anyway, only and always in the case of a user who signs up from an invite, i.e. ininvitations/edit
.name
androle
, but that did work out either.Here are some approaches I refrained from employing:
@user.assign_attributes(user_params)
instead of@user.update(user_params)
in theupdate
action of the controller that handles the step-by-step signup flow: I am concerned about unforeseen ramifications of such a solution.update
action of theInvitationsController
by using theupdate_columns
method, as again, I am concerned about unforeseen ramifications of skipping all validations and callbacks.Am I missing something obvious, is this an known issue, or is there a bug somewhere? I am ears for suggestions to fix or work around this.