我正在学习如何使用 Rails 3 和Agile Web Development Book, Task H发送电子邮件。但是,我不断收到以下错误:
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 配置的类似讨论,在这里和那里使用setup_mail.rb,但无法删除错误。
我的 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 具有带有 Notifier 行的 def create 方法:
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 可以。