I have models called User and Invite in my Rails 4 app.
I allow users to invite others to their project teams.
My user model has an attribute called :email.
I have an invite model for this purpose, it has the following attributes:
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"
When a user invites another person to join a project, they fill out a form that collects the above information.
Then in my invites controller, I'm trying to check if the email address entered is already an email address associated with any user's account.
In my Invites Controller, I have:
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
The recipient_status_known method is my attempt at checking if the email that is inserted in the new invites form is already in the database on the user table (saved as email).
If it is known, then the create action should assign the known user id as the recipient id on the invite table.
If it is not known, then I want to send the invited person an email inviting them to create an account.
How can I fix my test in the invite controller, to check if the User table has an email address provided in the new invite form?
My next attempt is to update my invite controller as follows:
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
end
My method to check if the email address added to the invite table is the same as any existing user member, is:
def recipient_status_known
User.exists?(email: @invite.email)
end
When I save this and try to submit the new invite form, I get no errors, but I get an error with the redirect in the last line of this code. That means that the email is not being found as a matching 'recipient status known' address. I know the email address that I am entering in the new invite form does match an email address in the user's table.
Any ideas on what I can try next?