Reputation: 553
I have the ability to send email to a user if a certain user has requested interest in their product. In my email I want to include certain information that is pulled from users but at the moment I am getting errors because it states that they are undefined despite these lines being used elsewhere in my application. I shall copy the email below and anything between the <% %> is what I wish to include and was wondering if anyone could point me in the right direction and tell me which are those that are correct and are not. Any help would be wonderful. The message I want to send is as followed:
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 %>.
I hope that makes sense. In order to give further insight in to what I have, I have a users table that includes user information and a game table that has games information with a user_id foreign key. Users have a has_many with games and games belongs_to user.
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
Upvotes: 0
Views: 562
Reputation: 7403
According to http://guides.rubyonrails.org/action_mailer_basics.html, action mailer "inherits from Abstract Controller", but not from your ApplicationController, so I wouldn't expect current_user
to be defined. Also, where's user
coming from? Just like controllers and their relevant views, the common practice is to define instance variables like @user
, @game
, etc. in your mailer, and then access these in the views.
If you think about it, it doesn't make sense to have a current_user
here, because if you start sending lots of emails, you'll want to make email sending to an asynchronous (e.g. with delayed_job or resque). In that case, there is no current user making a request, since it's no longer part of the request/response cycle.
Upvotes: 1