[英]rake aborted! ActionView::Template::Error: undefined method 'challenges' for nil:NilClass
I run this rake: 我跑这把耙子:
task monthly_report: :environment do
User.all.each do |user|
if user.challenges.present? && user.challenges.any?
UserMailer.monthly_report(user).deliver_now
end
end
end
Now maybe in the each
method itself is there a way to not send to user's who don't have any challenges? 现在也许在
each
方法本身中都有一种方法可以不发送给没有任何挑战的用户?
I keep getting this error: 我不断收到此错误:
ActionView::Template::Error: undefined method 'challenges' for nil:NilClass
after I run in production
heroku run rake monthly_reportActionView :: Template :: Error:
after I run in production
运行Herokuafter I run in production
nil:NilClass的未定义方法'challenges'
A user has_many
challenges and challenges belongs_to
user. 用户
has_many
挑战和挑战belongs_to
用户。
user_mailer.rb user_mailer.rb
def monthly_report(user)
@user = user if user.email.present?
mail to: user.email, subject: "Sorry! Please Ignore | Testing Email - Monthly Challenges Report"
end
monthly_report.html.erb monthly_report.html.erb
Accomplished: <%= @user.challenges.accomplished.count %> Ongoing: <%= @user.challenges.unaccomplished.count %>
<% if @user.challenges.accomplished.present? %>
<% @user.challenges.accomplished.order("deadline ASC").each do |challenge| %>
<% if challenge.date_started != nil %>
<%= link_to challenge_url(challenge) do %>
<%= challenge.name %> <%= challenge.committed_description %> for <%= challenge.days_challenged %> Days
<% end %>
<% if challenge.deadline.present? %>
<span style="display: inline; padding: .2em .6em .3em; font-size: 75%; font-weight: bold; line-height: 1; color: #fff; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: .25em; background-color: #446CB3; text-decoration: none;"><%= challenge.deadline.strftime("%b %d, %Y") %></span>
<% end %>
<% else %>
<%= link_to challenge_url(challenge) do %>
<%= challenge.name %>
<% end %>
<% if challenge.deadline.present? %>
<span style="display: inline; padding: .2em .6em .3em; font-size: 75%; font-weight: bold; line-height: 1; color: #fff; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: .25em; background-color: #446CB3; text-decoration: none;"><%= challenge.deadline.strftime("%b %d, %Y") %></span>
<% end %>
<% end %>
<% end %>
<% else %>
None.
<% end %>
Please update your rake task 请更新您的耙任务
task monthly_report: :environment do
User.all.each do |user|
if user.email.present? && user.challenges.present? && user.challenges.any?
UserMailer.monthly_report(user).deliver_now
end
end
end
I think error raising if your user dose not have any email in th record try this 我认为如果您的用户在记录中没有任何电子邮件,则会引发错误,请尝试此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.