简体   繁体   English

耙子流产了! ActionView :: Template :: Error:nil:NilClass的未定义方法“挑战”

[英]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_report ActionView :: Template :: Error: after I run in production运行Heroku after 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 %>&nbsp;&nbsp;
        <% 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 %>&nbsp;&nbsp;
                <% 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.

相关问题 ActionView :: Template :: Error:未定义的方法&#39;&lt;&#39;为nil:NilClass - ActionView::Template::Error: undefined method `<' for nil:NilClass ActionView :: Template :: Error:ActionView :: Template :: Error:未定义的方法`[]&#39;为nil:NilClass - ActionView::Template::Error: ActionView::Template::Error: undefined method `[]' for nil:NilClass 耙子流产了! 未定义的方法“空”? 对于nil:NilClass - rake aborted! undefined method `empty?' for nil:NilClass 耙子流产了! nil:NilClass的未定义方法“ each” - rake aborted! undefined method `each' for nil:NilClass 耙子流产了! nil:NilClass的未定义方法“ []” - rake aborted! undefined method `[]' for nil:NilClass 耙子流产了! nil的未定义方法“先决条件”:NilClass - rake aborted! undefined method `prerequisites' for nil:NilClass 错误ActionView :: Template :: Error(nil:NilClass的未定义方法“名称”) - Error ActionView::Template::Error (undefined method `name' for nil:NilClass) 获取错误 ActionView::Template::Error(nil:NilClass 的未定义方法“each”) - Getting error ActionView::Template::Error (undefined method `each' for nil:NilClass) ActionView :: Template :: Error(nil:NilClass的未定义方法“ strip!”) - ActionView::Template::Error (undefined method `strip!' for nil:NilClass) Heroku ActionView :: Template :: Error(nil:NilClass的未定义方法“名称”) - Heroku ActionView::Template::Error (undefined method `name' for nil:NilClass)
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM