В моем приложении Rails, у меня есть Invite модель:Rails 4: отображения активной ссылки в представлении почтовой
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 [email protected] (57.6ms)
Date: Tue, 15 Sep 2015 10:50:13 -0700
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
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
у меня было две проблемы здесь:
Я не понимаю, почему у меня есть
/account/sign_up?invite_token=cf7fb2ab8ad0774a99c1cdf7baf0bd44796c2f0f
в электронной почте и неhttp://localhost:3000/calendars/3/account/sign_up?invite_token=cf7fb2ab8ad0774a99c1cdf7baf0bd44796c2f0f
Я не уверен, что приведенная выше ссылка, порожденное
<%= @link %>
, кликабельна. И когда я пытаюсь сделать его интерактивным, с<%= link_to @link %>
, as recommended here, я получаю следующее сообщение об ошибке:ActionController :: UrlGenerationError в Приглашает # не создавать нет соответствующего маршрута {: действие => "индексных"}
You нас пригласили присоединиться к календарю. Нажмите здесь, чтобы просмотреть этот календарь: <% = link_to @link%>
С ошибкой идет от линии Click here to view this calendar: <%= link_to @link %>
Я очень признателен за любое понимание в отношении вышеуказанных пунктов.
Любая идея?
'@link = new_user_registration_url invite_token: invite.token' вместо' @link = new_user_registration_path invite_token: invite.token' – psantos