Anyone have experience with devise_invitable?

2020-06-28 10:35发布

问题:

How can you add the ability to use devise_invitable in the controller. I wan to add additional fields like message, proeject_id along with the invitation but have no idea where to start?

Here's devise_invitable: link text

回答1:

The devise_inevitable engine defines two view files and one controller. You more than likely do not need to perform any modifications to the controller that it defines. The form that sends invitations to users is defined here and you would override the rendering of the view like so:

#app/views/invitations/new.html.erb
<% form_for resource_name, resource, :url=> invitation_path(resource_name) do |f| %>
  <!-- Totally sweet new user invitation code goes here -->
<% end %>

The form that contains the code for users to complete registration after clicking the link on their email is here and you would override the rendering of it like so:

#app/views/invitations/edit.html.erb
<% form_for resource_name, resource, :url=> invitation_path(resource_name), :html=>{:method => :put } do |f| %>
  <!-- Totally sweet new user registration information goes here. -->
<% end %>

The first view is what will actually create the resource object, so that is more than likely where you'll want to be setting a message for the user to see, what project they are invited to as well. You might also want to override app/views/devise_mailer/invitation.html.erb to change the email message the user receives.

In order to override the invitations controller you'd need to do this:

#app/controllers/devise/invitations_controller.rb
class Devise::InvitationsController < ApplicationController
  def create
    #totally rad create stuff here.
  end
end

Devise and its extensions are all Rails engines, so the request will first look for an appropriate controller/model/view/helper file in your app directory, then in vendor/gems, and then finally in the gem directory that defines the engine.