ActionMailer消息传递内容

时间:2012-03-09 13:22:09

标签: ruby-on-rails actionmailer

如果某个用户请求对其产品感兴趣,我可以向用户发送电子邮件。在我的电子邮件中,我想要包含从用户那里提取的某些信息,但目前我收到错误,因为它声明它们未定义,尽管这些行在我的应用程序的其他地方使用。我将复制下面的电子邮件和< %%>之间的任何内容。是我想包括的,并想知道是否有人能指出我正确的方向,并告诉我哪些是正确的,不是。任何帮助都会很精彩。我要发送的消息如下:

Hello <%@user.username%>

The user <% current_user.username %> has registered an interest in the following product of  yours:

<% @game.game_name %>
<% @game.console %>
<% @game.genre %>

The user <% current_user.usernames %> has the following games for offer:

<% current_user.game.game_name %>
<% current_user.game.game_name %>
<% current_user.game.game_name %>

To view <% current_user.username %> profile click <% link_to "here", current_user.show %>
If you wish to contact the user by email then contact the following email <% current_user.email %>.

我希望这是有道理的。为了进一步了解我的内容,我有一个用户表,其中包含用户信息和一个游戏表,其中包含带有user_id外键的游戏信息。用户拥有has_many游戏和游戏belongs_to user。

更新

  class GameTrade < ActionMailer::Base
    default :from => "christopher@aol.com"

    def game_interest(user)
      @user = user
      @game = game
      mail :to => user.email, :subject => "Game Interest"
    end
  end

1 个答案:

答案 0 :(得分:1)

根据http://guides.rubyonrails.org/action_mailer_basics.html,动作邮件程序“继承自Abstract Controller”,但不是来自ApplicationController,因此我不希望定义current_user。另外,user来自哪里?就像控制器及其相关视图一样,通常的做法是在邮件程序中定义实例变量,如@user@game等,然后在视图中访问它们。

如果您考虑一下,在此处设置current_user是没有意义的,因为如果您开始发送大量电子邮件,您将需要将电子邮件发送到异步(例如,使用delayed_job或resque)。在这种情况下,没有当前用户发出请求,因为它不再是请求/响应周期的一部分。

相关问题