0

我的 Rails 4 应用程序中有名为 User 和 Invite 的模型。

我允许用户邀请其他人加入他们的项目团队。

我的用户模型有一个名为 :email 的属性。

为此,我有一个邀请模型,它具有以下属性:

create_table "invites", force: :cascade do |t|
    t.string   "email"
    t.integer  "project_id"
    t.integer  "sender_id"
    t.integer  "recipient_id"
    t.string   "token"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.boolean  "accepted",     default: false
    t.date     "expiry"

当用户邀请其他人加入项目时,他们会填写收集上述信息的表格。

然后在我的邀请控制器中,我试图检查输入的电子邮件地址是否已经是与任何用户帐户关联的电子邮件地址。

在我的邀请控制器中,我有:

class InvitesController < ApplicationController
  before_action :get_project
  before_action :recipient_status_known, only: :create

  def index
    @invites = @project.invites
  end

  def new
    # @invite = Invite.new
    @invite = @project.invites.build
  end

  def create
    @invite = Invite.new(invite_params)
    @invite.sender_id = current_user.profile.id

    # @project = Project.find(params[:project_id])

    if @invite.save && @recipient_status_known
        @invite.recipient_id = @invite.email.user.id  

        #send existing user email invitation to join project team
        InviteMailer.existing_user_invite(@invite).deliver_later 
        format.html { redirect_to @project, notice: 'Invitation sent'  }

         #Add the user to the user group - inivte rsvp pending
         @invite.recipient.project.push(@invite.project)
      elsif @invite.save && !@recipient_status_known
        InviteMailer.new_user_invite(@invite).deliver_later 
        format.html { redirect_to @project, notice: 'Invitation sent'  }
      else
        #send new user email invitation to join site and this project team
         @invite.recipient.project.push(@invite.project)

         # InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
      end

       # oh no, creating an new invitation failed

  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_invite
      @invite = Invite.find(params[:id])
    end

    def recipient_status_known
      User.persisted?(email: @invite.email)
    end

    def get_project
      @project = Project.find(params[:project_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def invite_params
      params[:invite].permit(:email, :project_id, :recipient_id, :token, :accepted, :expiry, :message)
    end
end

recipient_status_known 方法是我尝试检查插入到新邀请表单中的电子邮件是否已经在用户表的数据库中(另存为电子邮件)。

如果已知,则创建操作应将已知用户 ID 分配为邀请表上的收件人 ID。

如果不知道,那么我想向受邀者发送一封电子邮件,邀请他们创建一个帐户。

如何在邀请控制器中修复我的测试,以检查用户表是否有新邀请表单中提供的电子邮件地址?

我的下一个尝试是按如下方式更新我的邀请控制器:

after_filter :recipient_status_known, only: :create

def create
      @invite = Invite.new(invite_params)
      @invite.sender_id = current_user.profile.id

if @invite.save && @recipient_status_known
     @invite.recipient_id = @invite.email.user.id   
     InviteMailer.existing_user_invite(@invite).deliver_later 
     format.html { redirect_to @project, notice: 'Invitation sent'  }
     @invite.recipient.project.push(@invite.project)

elsif @invite.save && @recipient_status_known == false
  # InviteMailer.new_user_invite(@invite).deliver_later 
    format.html { redirect_to @project, notice: 'Invitation sent'  }
else
    format.html { redirect_to @project, notice: 'Something went wrong'  }
end

结尾

我检查添加到邀请表的电子邮件地址是否与任何现有用户成员相同的方法是:

def recipient_status_known
        User.exists?(email: @invite.email)
    end

当我保存它并尝试提交新的邀请表单时,我没有收到任何错误,但我在此代码的最后一行中收到了重定向错误。这意味着未找到该电子邮件作为匹配的“已知收件人状态”地址。我知道我在新邀请表单中输入的电子邮件地址与用户表中的电子邮件地址匹配。

关于下一步我可以尝试什么的任何想法?

4

1 回答 1

0

我会尝试使用这样的东西

def recipient_status_known(email)
    User.exists?(email: email)
end

recipient_status_known方法用于before_action. 在方法中设置recipient_status_known之前调用方法可能会发生。@invitecreate

建议更改使用参数email将导致错误。

基于此:https ://stackoverflow.com/a/16682728/1702557

于 2016-09-06T04:18:15.473 回答