开发者

Can't access field in record

I'm trying to use the suffix column of my class but I keep getting this error.

undefined method `suffix' for nil:NilClass

message_mailer.rb

class MessageMailer < ActionMailer::Base
  default :from => "noreply@cs.tsu.edu"

  def message_sender(user)
    @user = user
    carrier= user.carrier
    sms=user.telephone +carrier.suffix
    attachments["smalltsulogo.png"] = File.read("#{Rails.root}/public/images/smalltsulogo.png")
    mail(:to => "#{user.login} <#{user.email}>", :subject => "New Messa开发者_如何学Cge from cs.tsu.edu")
    mail(:to => "#{user.login} <#{sms}>", :subject => "New Message from cs.tsu.edu")
  end
end

CreateCarriers migration

class CreateCarriers < ActiveRecord::Migration
  def self.up
    create_table :carriers do |t|
      t.string :name
      t.string :suffix

      t.timestamps
    end
  end

  def self.down
    drop_table :carriers
  end
end

add index to carrier

class AddIndexToCarrier < ActiveRecord::Migration
  def self.up
    add_index :carriers, :suffix
  end

  def self.down
    remove_index :carriers, :suffix
  end
end


I believe what Sam is saying is make sure that you are actually passing an instance of the user model into your message_sender function. To expand on Sam's example:

def message_sender(user)
   if user.class != User
       #your code
   else
       puts 'Error: input is not a User Object'
   end
end

This code will print the error if the user variable passed in is not of the class User. IF this does print the error message that means the problem is likely wherever you are calling message_sender() in your application. Check that you are passing a valid user object to message_sender(). All credit goes to Sam for answering. (If I was not a new user I would have just commented on his post).


Make sure user is actually an object.

def message_sender(user)
   if user
       #your code
   else
       puts 'Error: User is nil'
   end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜