【问题标题】:Using Routing helpers in a Rake task在 Rake 任务中使用路由助手
【发布时间】:2011-02-23 01:19:41
【问题描述】:

我有一个 rake 任务,它会发出下一个“x”邀请加入测试版,它使用以下代码:

desc "This will send out the next batch of invites for the beta"
task :send_invites => :environment 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

我需要将“register_url”传递给邮件程序,以便链接显示在电子邮件中,但由于这是从 rake 任务而不是从请求中运行的,因此它无法访问帮助程序方法。实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: ruby-on-rails routing rake actionmailer


    【解决方案1】:

    您是否使用 ActionMailer 来传递消息?好像是这样。如果是这样,您可以传递令牌并让邮件程序本身使用 register_url 格式化令牌。所以你会这样做:

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

    然后在您的邀请模板中使用

    <%= register_url(i.token) %>
    

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 2010-11-29
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      相关资源
      最近更新 更多