ruby on rails - How can I invite users (using devise_invitable) and populate additional fields during the invite process? -


for example, when go users/invitations/new, field :email. i'd invite user and, in addition providing email, provide:

  • first_name
  • last_name
  • role
  • company (user belongs_to company)

i created users::invitationscontroller < devise::invitationscontroller:

class users::invitationscontroller < devise::invitationscontroller    private    def resource_params      params.permit(user: [:email, :invitation_token, :role, :company_id])[:user]    end end 

and added fields users/invitations/new. invitation sends fine, when accept , input password, validation fails saying no role selected (b/c of validation).

how can set these fields before sending invite , have them persist , save when invite accepted? thanks!

rails 5

here solution using accepts_nested_attributes_for. if custom attributes directly on user model should able replace profile_attributes: [:first_name, :last_name] :first_name, :last_name, :role, :company.

here controller.

class invitationscontroller < devise::invitationscontroller   before_action :update_sanitized_params, only: :update    # put /resource/invitation   def update     respond_to |format|       format.js         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         super       end     end   end     protected    def update_sanitized_params     devise_parameter_sanitizer.permit(:accept_invitation, keys: [:password, :password_confirmation, :invitation_token, profile_attributes: [:first_name, :last_name]])   end end 

inside form

<%= f.fields_for :profile |p| %>     <div class="form-group">       <%= p.label :first_name, class: 'sr-only' %>       <%= p.text_field :first_name, autofocus: true, class: 'form-control', placeholder: 'first name' %>     </div>      <div class="form-group">       <%= p.label :last_name, class: 'sr-only' %>       <%= p.text_field :last_name, class: 'form-control', placeholder: 'last name' %>     </div>   <% end %> 

in user.rb have

... accepts_nested_attributes_for :profile, reject_if: proc { |attributes| attributes[:first_name].blank? } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -