2012-03-09 79 views
0

我有能力发送电子邮件给用户,如果某个用户已请求对他们的产品感兴趣。在我的电子邮件中,我想包含某些从用户那里获得的信息,但是此刻我收到了错误,因为它声明它们未定义,尽管这些行在我的应用程序的其他地方使用。我将复制下面的电子邮件和任何介于<%%之间的内容是我想包括的内容,并且想知道是否有人可以指引我正确的方向,并告诉我哪些是正确的,哪些是不正确的。任何帮助将是美好的。我想发送的消息如下: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外键的游戏信息的游戏表。用户拥有游戏和游戏belongs_to用户的has_many。

UPDATE

class GameTrade < ActionMailer::Base 
    default :from => "[email protected]" 

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

你能在这里包括你的邮件方法吗?我想看看你是如何设置@游戏。 – 2012-03-09 17:35:18

+0

嗨本,我用我的邮件方法更新了我的帖子。我确实在'@user = user'下面有一行说'@game = game',但这不起作用。 – user1222136 2012-03-09 18:35:03

+0

没错,所以你不会在game_interest邮件函数中设置'@ game',所以它不会在你的视图中设置。您可以将'game'作为方法参数传递,或者以其他方式传递(如@ user.game,如果这是您应用程序中的关系) – 2012-03-09 21:10:14

回答

1

http://guides.rubyonrails.org/action_mailer_basics.html,行动邮件“从抽象控制器继承”,而不是从你的ApplicationController,所以current_user来定义我不会期望。另外,user从哪里来?就像控制器及其相关视图一样,通常的做法是在您的邮件程序中定义像@user,@game等实例变量,然后在视图中访问这些变量。

如果你仔细想想,在这里有一个current_user是没有意义的,因为如果你开始发送大量的电子邮件,你会想要发送电子邮件到一个异步的(例如delayed_job或resque)。在这种情况下,目前没有用户提出请求,因为它不再是请求/响应周期的一部分。

+0

我已更新@user部分,因为我在输入时意外排除了这一点。这部分工作正常,但游戏部分不起作用。我不断收到一个错误,说明未定义的方法“列的名称”,所以未定义的方法'game_name'等。 – user1222136 2012-03-09 17:27:41

+0

现在全部排序。 – user1222136 2012-03-11 00:14:46