Question

I am learning to how send email with Rails 3 with Agile Web Development Book, Task H. However, I keep getting the following error:

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'

I have looked at similar discussions on gmail configuration, using setup_mail.rb here and there, but wasn't able to remove the error.

My config/environment.rb file (because i want same for dev/test/production)has my gmail details with xxxx and 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 has this:

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.

and finally, models/controller/orders_controller has def create method with the line 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

I feel my email configuration was probably not done correctly, but not sure which one. Thanks!

EDIT: I managed to solve it! Instead of smtp i used sendmail. As for the number of arguments error, the app/mailers/notifer.rb looks like this:

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

Although my emails and everything still works, i'm curious whether anyones know why the smtp doesn't work, and sendmail does.

Was it helpful?

Solution

You've got a space in your order_received definition:

def order_received (order)

That should be this:

def order_received(order)

OTHER TIPS

Is it this line in create?

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

If that's what you really have then Ruby will try to pass whatever Cart.destroy returns to @order.save as an argument, the above is equivalent to this:

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

However, the save method doesn't take any arguments so you get a "wrong number of arguments (1 for 0) in OrdersController#create" error message. I'd guess that you mean:

if @order.save
  Cart.destroy(session[:cart_id])
  # etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top