【问题标题】:Sending email with Rails 3 error发送带有 Rails 3 错误的电子邮件
【发布时间】:2011-08-30 11:49:16
【问题描述】:

我正在学习如何使用 Agile Web Development Book, Task H 使用 Rails 3 发送电子邮件。但是,我不断收到以下错误:

ArgumentError in OrdersController#create

wrong number of arguments (1 for 0)
Rails.root: /Applications/XAMPP/xamppfiles/htdocs/rails_projects/TUTORIALS/depot

Application Trace | Framework Trace | Full Trace
app/mailers/notifier.rb:4:in `order_received'
app/controllers/orders_controller.rb:57:in `block in create'
app/controllers/orders_controller.rb:52:in `create'

我在gmail configuration, here 和there 上查看了关于gmail configuration, 的类似讨论,但无法消除错误。

我的 config/environment.rb 文件(因为我希望 dev/test/production 也一样)有我的 gmail 详细信息,其中包含 xxxx 和 yyyyy:

 Depot::Application.configure do
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "gmail.com",
    :authentication => "plain",
    :user_name => "xxxxxx@gmail.com",
    :password => "yyyyyyy",
    :enable_starttls_auto => true
}
end

model/notifier/order_received.text.erb 有这个:

Dear <%= @order.name %>
Thank you for your recent order from The Pragmatic Store.
You ordered the following items:
<%= render @order.line_items %>
We'll send you a separate e-mail when your order ships.

最后,models/controller/orders_controller 有 def create 方法和 Notifier 行:

def create 
   @order = Order.new(params[:order]) 
   @order.add_line_items_from_cart(current_cart) 
   respond_to do |format| 
      if @order.save Cart.destroy(session[:cart_id]) 
          session[:cart_id] = nil 
          Notifier.order_received(@order).deliver 
          format.html { redirect_to(store_url, :notice => 'Thank you for your order.') }
      else 
          format.html { render :action => "new" } 
          format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
      end 
   end 
end

我觉得我的电子邮件配置可能没有正确完成,但不确定是哪一个。谢谢!

编辑:我设法解决了!而不是 smtp 我使用了 sendmail。 至于参数个数错误,app/mailers/notifer.rb 是这样的:

class Notifier < ActionMailer::Base
  default :from => 'Sam Ruby <depot@example.com>'

  def order_received(order)
    @order = order
    mail :to => order.email, :subject => 'Pragmatic Store Order Confirmation'
  end

  def order_shipped(order)
    @order = order
    mail :to => order.email, :subject => 'Pragmatic Store Order Shipped'
  end
end

虽然我的电子邮件和一切仍然有效,但我很好奇是否有人知道为什么 smtp 不起作用,而 sendmail 却可以。

【问题讨论】:

  • 这是完整的:def create @order = Order.new(params[:order]) @order.add_line_items_from_cart(current_cart) respond_to do |format| if @order.save Cart.destroy(session[:cart_id]) session[:cart_id] = nil Notifier.order_received(@order).deliver format.html { redirect_to(store_url, :notice =&gt; 'Thank you for your order.') } else format.html { render :action =&gt; "new" } format.xml { render :xml =&gt; @order.errors, :status =&gt; :unprocessable_entity } end end end
  • @Sayanne:请编辑您的答案并将create 操作中的代码放入其中,而不是留下评论。
  • @Ryan:谢谢!包括完整的创建方法并删除了空格!

标签: ruby-on-rails-3 gmail mailer


【解决方案1】:

order_received 定义中有一个空格:

def order_received (order)

应该是这样的:

def order_received(order)

【讨论】:

    【解决方案2】:

    create中的这一行吗?

    if @order.save Cart.destroy(session[:cart_id])
    

    如果这就是你真正拥有的,那么 Ruby 将尝试将 Cart.destroy 返回的任何内容作为参数传递给 @order.save,以上等价于:

    if(@order.save(Cart.destroy(session[:cart_id])))
    

    但是,save method 不接受任何参数,因此您在 OrdersController#create 中收到“错误数量的参数(1 代表 0)”错误消息。我猜你的意思是:

    if @order.save
      Cart.destroy(session[:cart_id])
      # etc.
    

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 2011-06-28
      • 2012-01-25
      • 2013-05-14
      • 1970-01-01
      • 2014-01-28
      • 2012-04-21
      • 1970-01-01
      • 2013-04-14
      相关资源
      最近更新 更多