我对 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' for nil:NilClass`
我做错什么了吗?或者我怎样才能正确地将@ticket 传递到邮件中?