如何以rails应用程序的形式向电子邮件联系人发送电子邮件?

时间:2014-04-02 00:45:17

标签: ruby-on-rails ruby email ruby-on-rails-4 actionmailer

我希望在我的应用中有一个表单,登录用户可以输入他们朋友的电子邮件地址,点击发送,然后它会向他们输入表单的电子邮件地址发送一封自动发送的电子邮件。这是我到目前为止所拥有的。当我点击按钮将你带到表格时,我就会保持不变,所以我也不知道还有什么不行。

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(邀请表)

    <%= 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 %>

我甚至无法访问邀请表,因为我收到了未初始化的常量错误。所以我知道它超越了所有。请帮忙

1 个答案:

答案 0 :(得分:0)

您需要Invitation型号。

rails generate model Invitation invited_by:string sent_at:timestamp

See the getting started guide