开发者

Using Routing helpers in a Rake task

开发者 https://www.devze.com 2022-12-31 03:50 出处:网络
I have a rake task that sends out the next \'x\' invitations to join a beta it uses this code: desc \"This will send out the next batch of invites for the beta\"

I have a rake task that sends out the next 'x' invitations to join a beta it uses this code:

desc "This will send out the next batch of invites for the beta"
task :send_invites => :en开发者_运维技巧vironment do
  limit = ENV['limit']
  c = 0
  invitation = Invitation.all(:conditions => { :sent_at => nil, :sender_id => nil }, :limit => limit).each do |i|
    Mailer.deliver_invitation(i, register_url(i.token))
    c.increment!
  end

  puts "Sent #{c} invitations."
end

I need to pass in the 'register_url' to the Mailer in order for the link to show up in the email, but since this is running from a rake task and not from a request it does not have a access to the helper methods. What is the best way of achieving this?


Are you using an ActionMailer to deliver the messages? It seems like it. If so, you can just pass off the token and have the Mailer itself format the token using the register_url. So you would do:

invitation = Invitation.all(:conditions => { :sent_at => nil, :sender_id => nil }, :limit => limit).each do |i|
    Mailer.deliver_invitation(i)
    c.increment!
end

and then in your invite template you would just use

<%= register_url(i.token) %>
0

精彩评论

暂无评论...
验证码 换一张
取 消