Вопрос

I am trying to send an object (user) to NotificationsMailer like this:

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

But I got this error:

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

My template:

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

In the real world:

User.rb

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

But, I want to test it using pry, how to send such a user object? why my attempt failed?

Это было полезно?

Решение

You are using hashes but you should be using objects to create object (I am assuming mass-assigment is turned off as by default) 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

or turn mass-assign off and do

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

Другие советы

Try this

> @user = User.create!(password: "123", email: "abc@example.com")
> @user.basic_profile.name = "Samir"
> NotificationsMailer.welcome_facebook_user(@user).deliver
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top