使用pry发送对象?

时间:2013-05-06 10:27:11

标签: ruby ruby-on-rails-3.2 pry

我正在尝试将对象(用户)发送到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?}

在现实世界中:

User.rb

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

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

2 个答案:

答案 0 :(得分:0)

试试这个

> @user = User.create!(password: "123", email: "abc@example.com")
> @user.basic_profile.name = "Samir"
> NotificationsMailer.welcome_facebook_user(@user).deliver

答案 1 :(得分:0)

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

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