Как правильно использовать devise_invitable с activeadmin? (вопросы для отправки писем) - PullRequest
0 голосов
/ 21 марта 2019

Я создаю приложение в rails с интерфейсом администратора, используя гем activeadmin.У меня есть модель пользователя, модель организации и модель учетной записи (полиморфная).Один и тот же пользователь может иметь разные типы учетных записей (тренер, команда или спортсмен), а организация имеет много учетных записей (они являются членами этой организации).

Только администратор может создавать организации и отправлять приглашения присоединиться к ним.У меня есть модель OrganizationInvitation в активном администраторе, и я пытаюсь использовать 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

При работе с различными типами учетных записей будут возникать небольшие проблемы, в случае, когда у пользователя уже есть учетная запись и т. Д.но сейчас я просто пытаюсь отправить электронное письмо, чтобы пригласить нового пользователя в организацию.Когда я проверяю его, пользователь создается в базе данных, но, очевидно, письмо не отправляется.Я выполнил базовую настройку devise_invitable и добавил эту строку в свой конфигурационный файл разработки, но он все равно не работает.

config.action_mailer.perform_deliveries = true

Я никогда раньше не использовал эти 2 драгоценных камня и чувствую, что делаю что-то не так, но не знаю что.Заранее спасибо!

Редактировать: Вот и логи

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)

Что касается конфигурации почтовой программы, я не уверен, к чему вы обращаетесь.Разве это не должно обрабатываться в конфигурации DeviseInvitable?

Конфиг в 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
...