gpt4 book ai didi

ruby-on-rails - rails 4 : display clickable link in mailer view

转载 作者:行者123 更新时间:2023-12-04 05:44:18 24 4
gpt4 key购买 nike

在我的 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

这是相应的邮件程序 View :

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 ,我收到以下错误:

    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 助手提供绝对路径,包括协议(protocol)和服务器名称。我发现在服务器上创建指向应用程序的链接时,我主要在电子邮件中使用这些。它们主要用于提供外部链接。 (想想电子邮件链接、RSS 以及 YouTube 视频“共享”部分下的复制和粘贴 URL 字段之类的内容。)

并且在邮件类中使用 link_to 是一件坏事

class InviteMailer < ApplicationMailer

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

end

邮件程序 View :

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

关于ruby-on-rails - rails 4 : display clickable link in mailer view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32593232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com