Rails 4: 在邮件视图中显示可点击 link

Rails 4: display clickable link in mailer view

在我的 Rails 应用中,我有一个邀请模型:

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

我使用 Invite 模型创建邀请,通过以下 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 是否可点击。当我尝试使用 <%= link_to @link %>as recommended here 使其可点击时,出现以下错误:

    ActionController::UrlGenerationError in Invites#create 没有路线匹配 {:action=>"index"}

    您受邀加入日历。 单击此处查看此日历:<%= link_to @link %>

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

如果您对上述项目有任何见解,我将不胜感激。

有什么想法吗?

使用new_user_registration_url代替new_user_registration_path

_path 助手提供站点根目录相对路径。大多数时候你应该使用它。

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

并且在邮件程序中使用 link_to class 是一件坏事

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