我对ror很新,我的应用程序遇到了一些问题:
我有票证模型和评论模型,
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
现在我想在创建评论时发送邮件: 在评论控制器中:
def create
@comment = Comment.new(params[:comment])
respond_to do |format|
if @comment.save
TicketMailer.ticket_commented(@comment).deliver
format.html { redirect_to @comment, notice: 'Comment was successfull created.' }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
然后在邮件中:
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
但是当我试着打电话时
<%= @ticket.title %>
在视图中,我收到此错误:undefined method
title'为nil:NilClass`
我做错了什么吗?或者我如何正确地将@ticket传递给邮件程序?
答案 0 :(得分:1)
在您的邮件程序中,您尝试通过提供评论的ID来查找故障单,将行@ticket = Ticket.find_by_id(@comment.id)
更改为@ticket = @comment.ticket