Reputation: 935
I am trying to code a weekly digest as the ones used by quora or medium.
I want to sent a weekly selection of promos to a verified list of suscribers. To do that, I plan to create the mailer, and a rake task that I will run weekly.
I keep getting an error about the template of @promos, which I have check to be in place.
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 => "[email protected]"
# 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 Console output
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]
What am I missing?
Upvotes: 0
Views: 53
Reputation: 5213
You need to do link_to promo_url(promo)
. The helper method promo_url
is a member route, so it requires an :id
, and Rails can't provide the id without an object to tell it what id to use.
Upvotes: 1