Ruby on rails Actionmailer中电子邮件的命名错误

Ruby on rails Actionmailer中电子邮件的命名错误,ruby-on-rails,ruby,actionmailer,Ruby On Rails,Ruby,Actionmailer,我是采购确认书的命名者。不确定我在这里遗漏了什么。 在/购买时的命名错误 nil:NilClass的未定义方法“email” purchase_confirmationapp/mailers/purchase_mailer.rb # en.purchase_mailer.purchase_confirmation.subject 8 # 9 def purchase_confirmation(purchase) 10 @greeting = "Hi" 11

我是采购确认书的命名者。不确定我在这里遗漏了什么。
在/购买时的命名错误 nil:NilClass的未定义方法“email”

purchase_confirmationapp/mailers/purchase_mailer.rb

     #   en.purchase_mailer.purchase_confirmation.subject
8   #
9   def purchase_confirmation(purchase)
10     @greeting = "Hi"
11     
12     mail to: purchase.email, subject: "Purchase Confirmation"
13   end
14 end


createapp/controllers/purchases_controller.rb

1 class PurchasesController < InheritedResources::Base
2 before_filter :authenticate_admin_user!, :only => [:index, :edit, :update, :destroy]
3         def create
4                 @purchase = Purchase.new(params[:purchase])
5   if @purchase.save
6           PurchaseMailer.purchase_confirmation(@purchase).deliver
7   redirect_to "/thankyou"
8 else
9 
10   render :action => "new"
11 end

在视图中使用这些变量之前,需要分配
实例变量
。在您的情况下,它的
purchase
变量。所以让mailer方法看起来像这样

 def purchase_confirmation(purchase)
    @greeting = "Hi"
    @purchase = purchase
    mail to: purchase.email, subject: "Purchase Confirmation"
  end
end
在视图中,您应该这样做

<%= @purchase.email %>

<%= @greeting %>, thanks for your purchase! We're on it! 

,谢谢您的购买!我们在上面!

另一个选项是使实例变量可供其他类访问:

 class PurchasesController < InheritedResources::Base
 #adding this will allow you to access the instance variable in other classes. 
 attr_accessor :purchase
2 before_filter :authenticate_admin_user!, :only => [:index, :edit, :update, :destroy]
3         def create
4                 @purchase = Purchase.new(params[:purchase])
5   if @purchase.save
6           PurchaseMailer.purchase_confirmation(@purchase).deliver
7   redirect_to "/thankyou"
8 else
9 
10   render :action => "new"
11 end
class PurchasesController[:索引、编辑、更新、销毁]
3定义创建
4@purchase=purchase.new(参数[:purchase])
5如果@purchase.save
6 PurchaseMailer.purchase\u确认(@purchase).deliver
7将_重定向到“/谢谢”
8其他
9
10呈现:操作=>“新建”
11完

第12行-删除变量前面的@符号:
邮寄至:purchase.email,主题:“购买确认”
现在我收到一个未定义的nil方法“purchase”:Nilclass@Andrei您能提供完整的错误跟踪吗?@Andrei我在原始帖子的底部添加了完整跟踪您刚刚重新发布了purchase\u mailer和purchases\u controller。在错误跟踪中还有其他什么吗?好的@benchwarmer-非常接近。无错误,且我的终端显示(3.1ms)提交事务已提交购买\u mailer/purchase\u confirmation.text.erb(0.6ms)已将邮件发送至(136ms)日期:2013年2月27日星期三13:02:23-0500发件人:orders@itcinema.com消息ID:主题:购买确认Mime版本:1.0内容类型:文本/普通;charset=UTF-8内容传输编码:7bit Hi,感谢您的购买!我们在上面!已重定向到308ms(ActiveRecord:4.5ms)中找到的已完成302。请签出此答案,并确保在完成所有步骤后重新启动服务器
<%= @purchase.email %>

<%= @greeting %>, thanks for your purchase! We're on it! 
 class PurchasesController < InheritedResources::Base
 #adding this will allow you to access the instance variable in other classes. 
 attr_accessor :purchase
2 before_filter :authenticate_admin_user!, :only => [:index, :edit, :update, :destroy]
3         def create
4                 @purchase = Purchase.new(params[:purchase])
5   if @purchase.save
6           PurchaseMailer.purchase_confirmation(@purchase).deliver
7   redirect_to "/thankyou"
8 else
9 
10   render :action => "new"
11 end