You need an Invitation
model.
rails generate model Invitation invited_by:string sent_at:timestamp
Question
I want to have a form in my app where a logged in user can input their friend's email addresses, click send, and it will send out an automated email to the email addresses that they entered into the form. Here is what I have so far. I get unitialized constant when I click on the button to take you to the form so I don't know what else isn't working too.
invitations_controller.rb
class InvitationsController < ApplicationController
def new
@invitation = Invitation.new
end
def create
@invitation = Invitation.new(params[:invitation])
@invitation.invited_by = current_user.invitation_token # set the sender to the current user
if @invitation.save
Mailer.invitation(@invitation, new_user_path(:invite_token => @invitation.invited_by)).deliver #send the invite data to our mailer to deliver the email
else
flash.now[:notice] = "Something went wrong"
redirect_to root_url
end
end
end
mailer.rb
class Mailer < ActionMailer::Base
default from: "donotreply@mysite.com"
def invitation(invitation, signup_url)
subject 'Invitation'
recipients @recipient_email
from 'donotreply@mysite.com'
body :invitation => invitation, :signup_url => signup_url
invitation.update_attribute(:sent_at, Time.now)
end
end
invitation.html.erb
Mailer#invitation
You are invited to join our beta!
<%= signup_url(@invitation.invited_by) %>
new.html.erb (invitation form)
<%= simple_form_for @invitation, :url => new_invitation_path do |f| %>
<%= f.hidden_field :invitation_token, :value => @invitation.invited_by %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.submit 'Send' %>
<% end %>
I can't even get to the invitation form because I get the uninitialized constant error. So for all I know it works beyond that. Help please
Solution
You need an Invitation
model.
rails generate model Invitation invited_by:string sent_at:timestamp