0

我的购买确认是 NoMethodError。不知道我在这里缺少什么。
NoMethodError at /purchases undefined method `email' for nil:NilClass

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 更改为购买我现在收到此错误

    _app_views_purchase_mailer_purchase_confirmation_text_erb

1 <%= @email.purchase %>
2 
3 <%= @greeting %>, thanks for your purchase! We're on it! 

    purchase_confirmationapp/mailers/purchase_mailer.rb

7   #   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

    Rendered purchase_mailer/purchase_confirmation.text.erb (2.1ms)

在 176 毫秒内完成 500 内部服务器错误

>>NoMethodError - undefined method `purchase' for nil:NilClass:
  app/views/purchase_mailer/purchase_confirmation.text.erb:1:in `_app_views_purchase_mailer_purchase_confirmation_text_erb___3663910894988497772_70151297381080'
  (gem) actionpack-3.2.8/lib/action_view/template.rb:145:in `block in render'
  (gem) activesupport-3.2.8/lib/active_support/notifications.rb:125:in `instrument'
  (gem) actionpack-3.2.8/lib/action_view/template.rb:143:in `render'
  (gem) actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
  (gem) actionpack-3.2.8/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  (gem) activesupport-3.2.8/lib/active_support/notifications.rb:123:in `block in instrument'
  (gem) activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  (gem) activesupport-3.2.8/lib/active_support/notifications.rb:123:in `instrument'
  (gem) actionpack-3.2.8/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  (gem) actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
  (gem) actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
  (gem) actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:45:in `render_template'
  (gem) actionpack-3.2.8/lib/action_view/renderer/template_renderer.rb:18:in `render'
  (gem) actionpack-3.2.8/lib/action_view/renderer/renderer.rb:36:in `render_template'
  (gem) actionpack-3.2.8/lib/action_view/renderer/renderer.rb:17:in `render'
  (gem) actionpack-3.2.8/lib/abstract_controller/rendering.rb:110:in `_render_template'
  (gem) actionpack-3.2.8/lib/abstract_controller/rendering.rb:103:in `render_to_body'
  (gem) actionpack-3.2.8/lib/abstract_controller/rendering.rb:88:in `render'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:701:in `block in collect_responses_and_parts_order'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:712:in `each'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:712:in `each_template'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:697:in `collect_responses_and_parts_order'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:636:in `mail'
  app/mailers/purchase_mailer.rb:12:in `purchase_confirmation'
  (gem) actionpack-3.2.8/lib/abstract_controller/base.rb:167:in `process_action'
  (gem) actionpack-3.2.8/lib/abstract_controller/base.rb:121:in `process'
  (gem) actionpack-3.2.8/lib/abstract_controller/rendering.rb:45:in `process'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:457:in `process'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:452:in `initialize'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:439:in `new'
  (gem) actionmailer-3.2.8/lib/action_mailer/base.rb:439:in `method_missing'
  app/controllers/purchases_controller.rb:6:in `create'
  (gem) actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  (gem) actionpack-3.2.8/lib/abstract_controller/base.rb:167:in `process_action'
  (gem) actionpack-3.2.8/lib/action_controller/metal/rendering.rb:10:in `process_action'
  (gem) actionpack-3.2.8/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  (gem) activesupport-3.2.8/lib/active_support/callbacks.rb:414:in `_run__3758076575822448262__process_action__1783438763289863484__callbacks'
  (gem) activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
  (gem) activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  (gem) activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
  (gem) actionpack-3.2.8/lib/abstract_controller/callbacks.rb:17:in `process_action'
  (gem) actionpack-3.2.8/lib/action_controller/metal/rescue.rb:29:in `process_action'
  (gem) actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  (gem) activesupport-3.2.8/lib/active_support/notifications.rb:123:in `block in instrument'
  (gem) activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  (gem) activesupport-3.2.8/lib/active_support/notifications.rb:123:in `instrument'
  (gem) actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  (gem) actionpack-3.2.8/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  (gem) activerecord-3.2.8/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  (gem) actionpack-3.2.8/lib/abstract_controller/base.rb:121:in `process'
  (gem) actionpack-3.2.8/lib/abstract_controller/rendering.rb:45:in `process'
  (gem) actionpack-3.2.8/lib/action_controller/metal.rb:203:in `dispatch'
  (gem) actionpack-3.2.8/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  (gem) actionpack-3.2.8/lib/action_controller/metal.rb:246:in `block in action'
  (gem) actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:73:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  (gem) actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:36:in `call'
  (gem) journey-1.0.4/lib/journey/router.rb:68:in `block in call'
  (gem) journey-1.0.4/lib/journey/router.rb:56:in `each'
  (gem) journey-1.0.4/lib/journey/router.rb:56:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:600:in `call'
  (gem) better_errors-0.3.2/lib/better_errors/middleware.rb:51:in `app_call'
  (gem) better_errors-0.3.2/lib/better_errors/middleware.rb:45:in `call'
  (gem) warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
  (gem) warden-1.2.1/lib/warden/manager.rb:34:in `catch'
  (gem) warden-1.2.1/lib/warden/manager.rb:34:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  (gem) rack-1.4.1/lib/rack/etag.rb:23:in `call'
  (gem) rack-1.4.1/lib/rack/conditionalget.rb:35:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/head.rb:14:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/flash.rb:242:in `call'
  (gem) rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
  (gem) rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/cookies.rb:339:in `call'
  (gem) activerecord-3.2.8/lib/active_record/query_cache.rb:64:in `call'
  (gem) activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  (gem) activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `_run__1428976172628906859__call__3073148256851134214__callbacks'
  (gem) activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
  (gem) activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  (gem) activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/reloader.rb:65:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  (gem) railties-3.2.8/lib/rails/rack/logger.rb:26:in `call_app'
  (gem) railties-3.2.8/lib/rails/rack/logger.rb:16:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/request_id.rb:22:in `call'
  (gem) rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
  (gem) rack-1.4.1/lib/rack/runtime.rb:17:in `call'
  (gem) activesupport-3.2.8/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  (gem) rack-1.4.1/lib/rack/lock.rb:15:in `call'
  (gem) actionpack-3.2.8/lib/action_dispatch/middleware/static.rb:62:in `call'
  (gem) railties-3.2.8/lib/rails/engine.rb:479:in `call'
  (gem) railties-3.2.8/lib/rails/application.rb:223:in `call'
  (gem) rack-1.4.1/lib/rack/content_length.rb:14:in `call'
  (gem) railties-3.2.8/lib/rails/rack/log_tailer.rb:17:in `call'
  (gem) rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service'
  /Users/russell/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/russell/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/russell/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
4

2 回答 2

1

您需要先分配 ,instance variable然后才能在视图中使用它们。在您的情况下,它的purchase变量。所以让邮件方法看起来像这样

 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! 
于 2013-02-27T17:21:35.537 回答
0

另一种选择是让您的实例变量可供其他类访问:

 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
于 2013-02-27T17:59:39.947 回答