1

Rails 3 with Agile Web Development Book、TaskHでメールを送信する方法を学んでいます。ただし、次のエラーが発生し続けます。

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'

私はあちこちでsetup_mail.rbを使用して、 Gmailの構成に関する同様の議論を見てきました、エラーを取り除くことができませんでした。

私のconfig/environment.rbファイル(dev / test / productにも同じものが必要なため)には、xxxxとyyyyyのGmailの詳細があります。

 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という行を持つdefcreateメソッドがあります。

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が機能するかどうか知りたいです。

4

2 に答える 2

2

定義にスペースがありますorder_received:

def order_received (order)

それはこれであるべきです:

def order_received(order)
于 2011-05-22T09:50:34.510 に答える
1

この行createですか?

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

Cart.destroyそれがあなたが本当に持っているものである場合、Rubyは引数として返されるものをすべて渡そうとします@order.save、上記はこれと同等です:

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

ただし、このsaveメソッドは引数を受け取らないため、「OrdersController#createで引数の数が間違っています(0の場合は1)」というエラーメッセージが表示されます。私はあなたが意味すると思います:

if @order.save
  Cart.destroy(session[:cart_id])
  # etc.
于 2011-05-23T07:05:05.550 に答える