Set User Role upon invite
rtamburro opened this issue · 5 comments
I have followed the docs and every SO post on this and I cannot figure it out. I need an admin to set a User's role upon invite.
According to the docs, it should be as simple as adding this into app/controllers/users/invitations_controller.rb:
class Users::InvitationsController < Devise::InvitationsController
before_action :configure_permitted_parameters
protected
# Permit the new params here.
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:invite, keys: [:email, :role])
end
end
I do this an nothing appears in app/views/users/invitations/new.html.erb. That looks like:
<h2><%= t "devise.invitations.new.header" %></h2>
<%= simple_form_for(resource, as: resource_name, url: invitation_path(resource_name), html: { method: :post }) do |f| %>
<%= f.error_notification %>
<% resource.class.invite_key_fields.each do |field| -%>
<div class="form-inputs">
<%= f.input field %>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit, t("devise.invitations.new.submit_button") %>
</div>
<% end %>
My routes.rb file is structured as so:
Rails.application.routes.draw do
resources :blocks
devise_for :users, :controllers => {registrations: 'registrations', invitations: 'users/invitations'}
resources :assignments
resources :games
resources :users, :only =>[:show]
root "home#show"
end
I have tested that the email is sent out properly and can confirm it does. Any ideas on why I can't set the role in the new.html.erb file? The role field should be populating, but it doesn't.
You must change view invitations/new to add role field too. Default view include only invite_key_fields, which are fields needed to look for existing user, by default only email.
So you must change view to add new fields, and add these new fields to devise_parameter_sanitizer as you did. It's explained in README, https://github.com/scambra/devise_invitable#label-Strong+Parameters, you must add fields to sanitizer when you customize views adding more fields to form. Also, you don't need to override controller, you can add it to application controller.
I can't believe I never just tried adding that...thank you!
I think there should be an example in the docs of what a custom view might look like. A lot of research into my problem never showed anyone trying that and I think it would be a big help.
@rtamburro can you send PR improving that README section or adding a new one?
Yes, I will do that by end of tomorrow. Thanks again!
Just submitted, this was my first PR. Please let me know if anything needs correction.