Ruby on rails 未定义的方法“title';for nil:mailer中的NilClass
我是ror的新手,我的应用程序遇到了一些问题: 我有一个票证模型和一个评论模型Ruby on rails 未定义的方法“title';for nil:mailer中的NilClass,ruby-on-rails,model,nested,Ruby On Rails,Model,Nested,我是ror的新手,我的应用程序遇到了一些问题: 我有一个票证模型和一个评论模型 class Ticket < ActiveRecord::Base attr_accessible :content, :title, :user, :priority, :category, :status validates_presence_of :content, :title, :category has_many :comments, dependent:
class Ticket < ActiveRecord::Base
attr_accessible :content, :title, :user, :priority, :category, :status
validates_presence_of :content, :title, :category
has_many :comments, dependent: :destroy
accepts_nested_attributes_for :comments
end
class Comment < ActiveRecord::Base
attr_accessible :content, :ticket_id, :user
belongs_to :ticket
end
然后在mailer中:
class TicketMailer < ActionMailer::Base
default from: "helpdesk@testing.com"
def ticket_commented(comment)
@comment = comment
@ticket = Ticket.find_by_id(@comment.id)
mail(:to => @comment.user, :subject => 'New comment')
end
end
类TicketMailer<%= @ticket.title %>
在视图中,我得到了以下错误:undefined method
title'for nil:NilClass`
我做错什么了吗?或者我怎样才能将@ticket正确地传递到邮件发送程序中 在您的邮件中,您试图通过提供评论id来查找票证,请将行
@ticket=ticket.find\u by\u id(@comment.id)
更改为@ticket=@comment.ticket
谢谢,非常有效!我不知道我能做@comment.ticket,即使是评论也是属于ticket的。很高兴我能帮忙,请看一下
<%= @ticket.title %>