0

我对 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 methodtitle' for nil:NilClass`

我做错什么了吗?或者我怎样才能正确地将@ticket 传递到邮件中?

4

1 回答 1

1

在您的邮件中,您试图通过提供评论的 id 来查找票证,将行更改@ticket = Ticket.find_by_id(@comment.id)@ticket = @comment.ticket

于 2013-06-19T21:04:54.353 回答