我希望能够在新用户注册时将新用户的IP地址发送到我的管理员帐户。我有:
class UserMailer < ActionMailer::Base
default :from => '"Admin" <support@mysite.com>'
def registration_confirmation(user)
@user = user #needed in order to have access to user variables
def client_ip
@client_ip = request.remote_ip
end
mail(:to => "#{user.name} <#{user.email}>", :subject => "Welcome to MySite").deliver!
mail(:to => '"Admin" <support@mysite.com>',
:subject => "New Member",
:body => "New member #{user.name} with email #{user.email} and ip: #{@client_ip} has just signed up!",
:content_type => "text/html")
end
end
我收到了错误:
UsersController中的NameError #create
未定义的局部变量或#
users_controller.rb
def create
@user = User.new(params[:user])
if @user.save
UserMailer.registration_confirmation(@user).deliver
sign_in @user
redirect_to @user
else
render 'new'
end
end
答案 0 :(得分:2)
类似的东西:
def create
@user = User.new(params[:user])
if @user.save
UserMailer.registration_confirmation(@user, request.remote_ip).deliver
sign_in @user
redirect_to @user
else
render 'new'
end
end
def registration_confirmation(user, client_ip = '0.0.0.0')
@user = user #needed in order to have access to user variables
mail(:to => "#{user.name} <#{user.email}>", :subject => "Welcome to MySite").deliver!
mail(:to => '"Admin" <support@mysite.com>',
:subject => "New Member",
:body => "New member #{user.name} with email #{user.email} and ip: #{client_ip} has just signed up!",
:content_type => "text/html")
end
您只能访问控制器/视图中的请求对象。因此,如果您想要访问它,则需要将值传递给邮件程序。