0

I am building an app in rails with an admin interface using the activeadmin gem. I have a User model, an Organization Model and an Account Model (polymorphic). A same user can have different types of accounts (Trainer, Team or Athlete) and an organization has many accounts (they are members of this organization).

Only the admin can create organizations and send invitations to join them. I have an OrganizationInvitation model in active admin and I am trying to use devise_invitable.

ActiveAdmin.register OrganizationInvitation do
  permit_params :organization_id, :email, :status
  form do |f|
    f.semantic_errors
    f.inputs do
      f.input :organization, collection: Organization.all.map { |organization| [organization.name, organization.id] }
      f.input :email
    end
    f.actions
  end

  controller do
    def create
      @organization_invitation = OrganizationInvitation.new(organization_invitation_params)
      @email = organization_invitation_params[:email]
      @user = User.invite!(email: @email)
      if @organization_invitation.save
        redirect_to admin_organization_invitations_path, notice: 'Invitation successfully sent'
      else
        redirect_to admin_organization_invitations_path, alert: 'Something went wrong'
      end
    end


    private

    def organization_invitation_params
      params.require(:organization_invitation).permit(
        :organization_id,
        :email
      )
    end
  end
end

There will be different small issues to handle the different types of accounts, the case in which a user already has an account etc but for now I am just trying to send an email to invite a new user to an organization. When I test it, the user gets created in the database but apparently no email gets sent. I did the basic setup for devise_invitable and I added this line in my development config file but it still does not work.

config.action_mailer.perform_deliveries = true

I never used these 2 gems before and feel I am doing sth wrong but do not know what. Thank you in advance!

Edit: Here are the logs as well

Started GET "/admin/organization_invitations" for 127.0.0.1 at 2019-03-21 16:10:44 +0000
Processing by Admin::OrganizationInvitationsController#index as HTML
  AdminUser Load (3.3ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/index.html.arb
   (1.1ms)  SELECT COUNT(*) FROM (SELECT  1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE  (0.0ms)  SELECT COUNT(*) FROM (SELECT  1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
   (0.4ms)  SELECT COUNT(*) FROM "organization_invitations"
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE  (0.0ms)  SELECT COUNT(*) FROM (SELECT  1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  OrganizationInvitation Load (0.8ms)  SELECT  "organization_invitations".* FROM "organization_invitations" ORDER BY "organization_invitations"."id" desc LIMIT $1 OFFSET $2  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Organization Load (1.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Organization Load (1.8ms)  SELECT "organizations".* FROM "organizations"
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/index.html.arb (194.2ms)
Completed 200 OK in 244ms (Views: 224.8ms | ActiveRecord: 13.5ms)


Started GET "/admin/organization_invitations/new" for 127.0.0.1 at 2019-03-21 16:10:46 +0000
Processing by Admin::OrganizationInvitationsController#new as HTML
  AdminUser Load (0.5ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/new.html.arb
  Organization Load (0.3ms)  SELECT "organizations".* FROM "organizations"
  ↳ app/admin/organization_invitations.rb:8
  Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/new.html.arb (105.3ms)
Completed 200 OK in 169ms (Views: 161.7ms | ActiveRecord: 0.8ms)


Started POST "/admin/organization_invitations" for 127.0.0.1 at 2019-03-21 16:10:51 +0000
Processing by Admin::OrganizationInvitationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"XkxCRz+/BsvXAmyKUZk4UZhjtE1AcrhOnCxheNQGPQVP+j6tf+rZYVqqiU+CxtYR57pPShgjJdknG9PmiNtGOA==", "organization_invitation"=>{"organization_id"=>"1", "email"=>"myaddress@gmail.com"}, "commit"=>"Create Organization invitation"}
  AdminUser Load (2.0ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  User Load (1.4ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "antoinecquellier@gmail.com"], ["LIMIT", 1]]
  ↳ app/admin/organization_invitations.rb:18
  User Load (0.9ms)  SELECT  "users".* FROM "users" WHERE "users"."invitation_token" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["invitation_token", "c94d3b1e55b5cf39eba111085ec94eb1dcccca321308b87017a7f08b1634a3df"], ["LIMIT", 1]]
  ↳ app/admin/organization_invitations.rb:18
   (0.3ms)  BEGIN
  ↳ app/admin/organization_invitations.rb:18
  User Update (1.6ms)  UPDATE "users" SET "invitation_token" = $1, "invitation_created_at" = $2, "invitation_sent_at" = $3, "updated_at" = $4 WHERE "users"."id" = $5  [["invitation_token", "c94d3b1e55b5cf39eba111085ec94eb1dcccca321308b87017a7f08b1634a3df"], ["invitation_created_at", "2019-03-21 16:10:51.702522"], ["invitation_sent_at", "2019-03-21 16:10:51.702522"], ["updated_at", "2019-03-21 16:10:51.704500"], ["id", 1]]
  ↳ app/admin/organization_invitations.rb:18
   (4.7ms)  COMMIT
  ↳ app/admin/organization_invitations.rb:18
  Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise_invitable-2.0.0/app/views/devise/mailer/invitation_instructions.html.erb
  Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise_invitable-2.0.0/app/views/devise/mailer/invitation_instructions.html.erb (2.4ms)
  Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise_invitable-2.0.0/app/views/devise/mailer/invitation_instructions.text.erb
  Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise_invitable-2.0.0/app/views/devise/mailer/invitation_instructions.text.erb (5.2ms)
Devise::Mailer#invitation_instructions: processed outbound mail in 291.5ms
Sent mail to antoinecquellier@gmail.com (10.4ms)
Date: Thu, 21 Mar 2019 16:10:52 +0000
From: please-change-me-at-config-initializers-devise@example.com
Reply-To: please-change-me-at-config-initializers-devise@example.com
To: antoinecquellier@gmail.com
Message-ID: <5c93b78c2ad5_9da3fc49695b254502d0@MBP-de-Antoine.mail>
Subject: Invitation instructions
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5c93b78c751_9da3fc49695b25450147";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_5c93b78c751_9da3fc49695b25450147
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Hello antoinecquellier@gmail.com

Someone has invited you to http://localhost:3000/, you can accept it through the link below.

http://localhost:3000/users/invitation/accept?invitation_token=LtbgEbDe4ac63rs7oeC3


If you don't want to accept the invitation, please ignore this email. Your account won't be created until you access the link above and set your password.

----==_mimepart_5c93b78c751_9da3fc49695b25450147
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Hello antoinecquellier@gmail.com</p>

<p>Someone has invited you to http://localhost:3000/, you can accept it through the link below.</p>

<p><a href="http://localhost:3000/users/invitation/accept?invitation_token=LtbgEbDe4ac63rs7oeC3">Accept invitation</a></p>


<p>If you don&#39;t want to accept the invitation, please ignore this email. Your account won&#39;t be created until you access the link above and set your password.</p>

----==_mimepart_5c93b78c751_9da3fc49695b25450147--

(0.2ms)  BEGIN
  ↳ app/admin/organization_invitations.rb:20
  Organization Load (0.4ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/admin/organization_invitations.rb:20
  OrganizationInvitation Create (1.5ms)  INSERT INTO "organization_invitations" ("organization_id", "email", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["organization_id", 1], ["email", "antoinecquellier@gmail.com"], ["created_at", "2019-03-21 16:10:52.023105"], ["updated_at", "2019-03-21 16:10:52.023105"]]
  ↳ app/admin/organization_invitations.rb:20
   (1.6ms)  COMMIT
  ↳ app/admin/organization_invitations.rb:20
Redirected to http://localhost:3000/admin/organization_invitations
Completed 302 Found in 345ms (ActiveRecord: 15.0ms)


Started GET "/admin/organization_invitations" for 127.0.0.1 at 2019-03-21 16:10:52 +0000
Processing by Admin::OrganizationInvitationsController#index as HTML
  AdminUser Load (0.5ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/index.html.arb
   (0.6ms)  SELECT COUNT(*) FROM (SELECT  1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE  (0.0ms)  SELECT COUNT(*) FROM (SELECT  1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
   (0.5ms)  SELECT COUNT(*) FROM "organization_invitations"
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE  (0.0ms)  SELECT COUNT(*) FROM (SELECT  1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  OrganizationInvitation Load (0.5ms)  SELECT  "organization_invitations".* FROM "organization_invitations" ORDER BY "organization_invitations"."id" desc LIMIT $1 OFFSET $2  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Organization Load (0.5ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.1ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.1ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  CACHE Organization Load (0.0ms)  SELECT  "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Organization Load (0.6ms)  SELECT "organizations".* FROM "organizations"
  ↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
  Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/index.html.arb (204.8ms)
Completed 200 OK in 240ms (Views: 233.9ms | ActiveRecord: 3.6ms)

Regarding the mailer config, I'm not sure what you're refering to. Should not this be handled in the DeviseInvitable config?

Config in development.rb

  config.action_mailer.default_url_options = { host: 'http://localhost:3000' }

  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: "587",
    domain: "gmail.com",
    user_name: "myaddress@gmail.com",
    password: "password",
    authentication: "plain",
    enable_starttls_auto: true
  }

  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.perform_deliveries = true

  config.action_mailer.perform_caching = false
Antoine Quellier
  • 152
  • 2
  • 11
  • Please show us your logs after the submission of a new user form. Have you configured the mailer? The user gets created, good, but we don't see where they get created. After they are created, do you actually create a `OrganizationInvitation` manually or does that happen automatically? – Veridian Dynamics Mar 21 '19 at 15:29
  • Hey @VeridianDynamics Thank you for your answer, I edited my question. The `OrganizationInvitation` gets created through the `Admin::OrganizationInvitationsController` at this line I think `if @organization_invitation.save` – Antoine Quellier Mar 21 '19 at 16:23
  • You *might* want to edit out your personal email address from the logs. In the logs I see sending mail: `Devise::Mailer#invitation_instructions: processed outbound mail in 291.5ms`. This means it successfully sent to that `@gmail` account. Did you check your spam folders? Are you sure that's the correct email? Also, this is how you configure your Gmail in `/config/environments/development.rb`: https://stackoverflow.com/questions/25872389/rails-4-how-to-correctly-configure-smtp-settings-gmail. Make sure you don't put any passwords in their manually. Use ENV variables for passwords / creds – Veridian Dynamics Mar 21 '19 at 16:29
  • Yep I checked my spams and tried with another email address as well but it still does not work. – Antoine Quellier Mar 21 '19 at 19:08
  • Did you add your configuration in the environment/development.rb file? – Veridian Dynamics Mar 21 '19 at 19:55
  • Yes the config looks like the last part of my question (edited), logs are still the same, no error – Antoine Quellier Mar 21 '19 at 23:06

0 Answers0