Rails 4:在邮件程序视图中显示可点击链接

时间:2015-09-15 18:35:42

标签: ruby-on-rails ruby-on-rails-4 model-view-controller actionmailer

在我的Rails应用程序中,我有一个Invite模型:

class Invite < ActiveRecord::Base
  belongs_to :calendar
  belongs_to :sender, :class_name => 'User'
  belongs_to :recipient, :class_name => 'User'

  before_create :generate_token

  def generate_token
    self.token = Digest::SHA1.hexdigest([self.calendar_id, self.recipient_role, Time.now, rand].join)
  end

end

通过以下迁移:

class CreateInvites < ActiveRecord::Migration
  def change
    create_table :invites do |t|
      t.string :email 
      t.integer :calendar_id
      t.integer :sender_id
      t.integer :recipient_id
      t.string :recipient_role
      t.string :token
      t.timestamps null: false
    end
  end
end

我使用邀请模型通过以下InvitesController

创建邀请
class InvitesController < ApplicationController
  def create
    @invite = Invite.new(invite_params) # Make a new Invite
    @invite.sender_id = current_user.id # set the sender to the current user
    @calendar = Calendar.find_by_id(@invite.calendar_id)
    authorize @calendar
    if @invite.save
      InviteMailer.invite(@invite).deliver #send the invite data to our mailer to deliver the email
    else
      format.html { render :edit, notice: 'Invitation could not be sent.' }
    end
    redirect_to calendar_path(@calendar)
  end

  private

  def invite_params
    params.require(:invite).permit(:email, :calendar_id)
  end

end

以下是InviteMailer

class InviteMailer < ApplicationMailer

  def invite(invite)
    @link = new_user_registration_path invite_token: invite.token
    mail to: invite.email, subject: "Calendy Invitation"
  end

end

这是相应的邮件程序视图:

You've been invited to join a calendar.
Click here to view this calendar: <%= @link %>

当我创建邀请并检查服务器上的日志时,我可以看到邮件已生成以下电子邮件:

InviteMailer#invite: processed outbound mail in 13.7ms

Sent mail to example@gmail.com (57.6ms)
Date: Tue, 15 Sep 2015 10:50:13 -0700
From: from@example.com
To: example@gmail.com
Message-ID: <55f85a5515500_1cf93ff0b4ef71144524c@XXX.local.mail>
Subject: Calendy Invitation
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<html>
  <body>
    You've been invited to join a calendar.
Click here to view this calendar: /account/sign_up?invite_token=cf7fb2ab8ad0774a99c1cdf7baf0bd44796c2f0f
  </body>
</html>

Redirected to http://localhost:3000/calendars/3

我有两个问题:

  1. 我不明白为什么我在电子邮件中有/account/sign_up?invite_token=cf7fb2ab8ad0774a99c1cdf7baf0bd44796c2f0f而不是http://localhost:3000/calendars/3/account/sign_up?invite_token=cf7fb2ab8ad0774a99c1cdf7baf0bd44796c2f0f

  2. 我不确定由<%= @link %>生成的上述链接是否可点击。当我尝试使用<%= link_to @link %>as recommended here进行点击时,我收到以下错误:

    Invites #create中的ActionController :: UrlGenerationError 没有路线匹配{:action =&gt;&#34; index&#34;}

    您已被邀请加入日历。 点击此处查看此日历:&lt;%= link_to @link%&gt;

  3. 错误来自第Click here to view this calendar: <%= link_to @link %>

    我非常感谢有关上述项目的任何见解。

    有什么想法吗?

1 个答案:

答案 0 :(得分:3)

使用new_user_registration_url代替new_user_registration_path

_path帮助程序提供站点根目录相对路径。你应该在大部分时间都使用它。

_url帮助程序提供绝对路径,包括协议和服务器名称。我发现在创建服务器上的应用程序链接时,我主要在电子邮件中使用这些。它们应主要在提供外部链接时使用。 (想想电子邮件链接,RSS,以及YouTube视频“分享”部分下的复制和粘贴网址字段。)

在邮件程序类中使用link_to是一件坏事

class InviteMailer < ApplicationMailer

  def invite(invite)
    @invite = invite
    mail to: invite.email, subject: "Calendy Invitation"
  end

end

梅勒观点:

Click here to view this calendar: <%= link_to "link", new_user_registration_url(invite_token: @invite.token) %>