Ruby on Rails Mailer中的操作视图错误

时间:2018-12-19 23:10:43

标签: ruby-on-rails ruby mailer

我正在尝试将每周摘要编码为法定人数或中级人数所使用的摘要。

我想每周发送一次精选促销到经过验证的订户列表中。为此,我计划创建邮件程序,并创建一个每周执行的rake任务。

我不断收到有关@promos模板的错误,我已经检查过该模板。

Task.rake

desc 'send digest email'
task send_weekly_email: :environment  do

  @promociones = Promo.where("validez <= ?", Time.zone.now).order("created_at DESC")
  @suscriptors = Suscriptor.where(email_confirmation: true)

  @suscriptors.each do |suscriptor|
      WeeklyDigestMailer.weekly_promos(suscriptor, @promociones).deliver_now
  end
end

WeeklyDigestMailer

class WeeklyDigestMailer < ApplicationMailer
  default :from => "info@adeter.org"
  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.weekly_digest_mailer.weekly_promos.subject
  #
  def weekly_promos(suscriptor, promos)
      @promos = promos
      mail(:to => "<#{suscriptor.email}>", :subject => "Mercadillo digital semanal de Guia#{ENV['CURRENT_CITY_CAP']}.es")
  end
end

weekly_promos.html.erb

<p>
  <% @promos.each do |promo| %>

      <% link_to promo_url do %>

      <p><h2><%= promo.title %></h2></p>
      <p><%= promo.texto %></p>
      <p><%= promo.imgpromo %></p>
      <p>hasta: <span><%= promo.validez %></span> </p> -->

     <% end %>

  <% end %>
</p>

Rake :: Task ['send_weekly_email']。execute 控制台输出

  Suscriptor Load (0.2ms)  SELECT "suscriptors".* FROM "suscriptors" WHERE "suscriptors"."email_confirmation" = $1  [["email_confirmation", "t"]]
  Rendering weekly_digest_mailer/weekly_promos.html.erb within layouts/mailer
  Promo Load (2.0ms)  SELECT "promos".* FROM "promos" WHERE (validez <= '2018-12-19 22:37:51.784767') ORDER BY "promos"."created_at" DESC, created_at DESC
  Rendered weekly_digest_mailer/weekly_promos.html.erb within layouts/mailer (15.4ms)
WeeklyDigestMailer#weekly_promos: processed outbound mail in 25.4ms
ActionView::Template::Error: No route matches {:action=>"show", :controller=>"promos"} missing required keys: [:id]

我想念什么?

1 个答案:

答案 0 :(得分:1)

您需要执行link_to promo_url(promo)。辅助方法promo_url是成员路由,因此它需要一个:id,并且Rails无法在没有对象告诉它使用什么ID的情况下提供该ID。