Ruby Mailer: Wrong number of arguments

做~自己de王妃 提交于 2020-01-03 16:12:19

问题


I'm working on building out my mailer, but I keep running into:

wrong number of arguments (0 for 1)

Call my crazy, but I feel like I defined everything correctly:

Controller (truncated for brevity):

def create
@cms484 = Cms484.new(cms484_params)

respond_to do |format|
  if @cms484.save
    SendLink.message(@cms484).deliver_later
    format.html { redirect_to cms484s_path, notice: 'Cms484 was successfully created.' }
    format.json { render :show, status: :created, location: @cms484 }
  else
    format.html { render :new }
    format.json { render json: @cms484.errors, status: :unprocessable_entity }
  end
end

SendLink.rb:

    class SendLink < ApplicationMailer
    def message(cms484)
    @cms484 = cms484
    mail(
      :subject => 'Hello from Postmark',
      :to  => @cms484.recipient ,
      :from => 'info@mysite.com',
      :html_body => '<strong>Hello</strong> user!.',
  end
end

Can anybody else see the needle in the haystack or am I missing something else entirely?

I'm using Postmark for delivery if that matters, and have those parameters defined in my application.rb file as per the documentation. Think this is a simpler matter though.

Edit The complete error:

Completed 500 Internal Server Error in 76ms

ArgumentError (wrong number of arguments (0 for 1)):
  app/mailers/send_link.rb:2:in `message'
  app/mailers/send_link.rb:4:in `message'
  app/controllers/cms484s_controller.rb:38:in `block in create'
  app/controllers/cms484s_controller.rb:36:in `create'

回答1:


I had a similar issue where I named my ActionMailer method "message" it turns out it was a reserved word in Rails and threw an error.

I would assume that "mail" was a reserved word where "email" was not.




回答2:


mail ... line in SendLink.rb looks wrong , change it to,

 mail(
  :subject => 'Hello from Postmark',
  :to  => @cms484.recipient ,
  :from => 'info@mysite.com',
  :html_body => '<strong>Hello</strong> user!.')



回答3:


Ok, so I decided to re-write it and behold - it works. Why or what's different than the previous version(other than the method email vs mail, surely that can't be it?), I have no idea. If you can see what it is, Please point it out to me!

Send_link.rb:

class SendLink < ApplicationMailer



def email(cms484)
    @cms484 = cms484
     mail(
  :subject => 'Hello from Postmark',
  :to  => @cms484.recipient ,
  :from => 'info@mysite.com',
)
  end
end

Controller:

def create
@cms484 = Cms484.new(cms484_params)

respond_to do |format|
  if @cms484.save
    SendLink.email(@cms484).deliver_later

    format.html { redirect_to cms484s_path, notice: 'Cms484 was successfully created.' }
    format.json { render :show, status: :created, location: @cms484 }
  else
    format.html { render :new }
    format.json { render json: @cms484.errors, status: :unprocessable_entity }
  end
end 

end



来源:https://stackoverflow.com/questions/30610626/ruby-mailer-wrong-number-of-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!