0

我正在尝试将对象(用户)发送到 NotificationsMailer,如下所示:

pry(main)>NotificationsMailer.welcome_facebook_user({:password=>'123',:email=>'as453now@gmail.com',:basic_profile=>{:name=>'Samir'}}).deliver

但我得到了这个错误:

ActionView::Template::Error: undefined method `basic_profile' for #<Hash:0x0000000786d588>
from /app/app/views/notifications_mailer/welcome_facebook_user.html.haml:2:in `block in _app_views_notifications_mailer_welcome_facebook_user_html_haml__4104442834642741128_62933320'

NotificationsMailer.welcome_facebook_user:

  def welcome_facebook_user(user)
    @user = user
    puts "SENT welcome_facebook_user TO:"
    puts user[:email]
    mail(:to=>user[:email], :subject => "Welcome!")
  end

我的模板:

=content_for :title do
  Welcome, #{@user.basic_profile.name unless @user.basic_profile.nil?}

在现实世界:

用户.rb

class User < Refinery::Core::BaseModel
has_one :basic_profile

但是,我想用 pry 测试它,如何发送这样的用户对象?为什么我的尝试失败了?

4

2 回答 2

0

尝试这个

> @user = User.create!(password: "123", email: "abc@example.com")
> @user.basic_profile.name = "Samir"
> NotificationsMailer.welcome_facebook_user(@user).deliver
于 2013-05-06T10:35:42.140 回答
0

您正在使用哈希,但您应该使用对象来创建对象(我假设默认情况下质量分配已关闭):

basic_profile = BasicProfile.new
basic_profile.name = 'Samir'
user = User.new
user.password = '123'
user.email = 'aba@gmail.com'
user.basic_profile = basic_profile
NotificationsMailer.welcome_facebook_user(user).deliver

或关闭质量分配并执行

NotificationsMailer.welcome_facebook_user(User.new({:password=>'123',:email=>'as453now@gmail.com',:basic_profile=>BasicProfile.new({:name=>'Samir'})})).deliver
于 2013-05-06T11:20:06.467 回答