简体   繁体   English

Rails ActionMailer错误未定义电子邮件

[英]Rails ActionMailer error undefined email

Beginner rails error I'm trying to send emails out to ALL current users when an article is updated. 初学者Rails错误我正在尝试在文章更新时向所有当前用户发送电子邮件。

I have sendgrid and devise set up with my app and am able to get the mailer to work through rails console. 我已经使用我的应用程序设置了sendgrid并设计了它,并且能够使邮件程序通过Rails控制台工作。 But, for some reason, I receive an undefined method email for #<User::ActiveRecord_Relation:0x007f8685aebec0> when updating an article. 但是,由于某种原因,在更新文章时,我收到undefined method email for #<User::ActiveRecord_Relation:0x007f8685aebec0>undefined method email for #<User::ActiveRecord_Relation:0x007f8685aebec0>

ArticleNotificationMailer ArticleNotificationMailer

    class ArticleNotificationMailer < ApplicationMailer
        default from: 'you@example.com'

      def new_article(user, article)
        @user = user
        @article = article

        mail(
          to: @user.email,
          subject: "New update to article #{article.title}"
          )
      end
    end

new_article.html.erb new_article.html.erb

    <!DOCTYPE html>
    <html>
      <head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-type" />
      </head>
      <body>
        <h1>New article on website "<%= @article.title %>"</h1>
        <p>
          <%= @article.body %>
        </p>
        <p>
          <%= link_to "View Comment on site", article_url(@article, anchor: "updates=#{@article.id}") %>
        </p>
      </body>
    </html>

ArticleController I'm using ArticleNotificationMailer.new_article(@user, @article).deliver ArticleController我使用ArticleNotificationMailer.new_article(@user, @article).deliver

      def update
        respond_to do |format|
          if @article.update(article_params)
            ArticleNotificationMailer.new_article(@user, @article).deliver
            format.html { redirect_to @article, notice: 'Article was successfully updated.' }
            format.json { render :show, status: :ok, location: @article }
          else
            format.html { render :edit }
            format.json { render json: @article.errors, status: :unprocessable_entity }
          end
        end
      end

Error Message 错误信息

NoMethodError in ArticlesController#update
undefined method `email' for #<User::ActiveRecord_Relation:0x007f8685aebec0>

mail(
  to: @user.email,
  subject: "New post to articles #{article.title}"
  )
end

Rails Console Rails控制台

>> u = User.last
>> a = Article.first
>> ActionNotificationMailer.new_article(u, a).deliver_now
 ArticleNotificationMailer.new_article(@user, @article).deliver

Seems like @user was initialized by User.where() in your controller. 好像@user是由控制器中的User.where()初始化的。 User.where returns an instance of User::ActiveRecord_Relation which is in fact rails-enhanced array. User.where返回User :: ActiveRecord_Relation的实例,该实例实际上是rails增强的数组。 And errors comes up when you are trying to call email on this array. 当您尝试在此阵列上调用电子邮件时,会出现错误。

Just use User.find if you need to find only one record. 如果只需要查找一条记录,只需使用User.find即可。

Try passing in the id of the elements. 尝试传递元素的ID。

class ArticleNotificationMailer < ApplicationMailer
        default from: 'you@example.com'

      def new_article(user_id, article_id)
        @user = User.where(id: user_id)
        @article = Article.where(id: article_id)

        mail(
          to: @user.email,
          subject: "New update to article #{article.title}"
          )
      end
    end


In your console 
>> u = User.last
>> a = Article.first
>> ActionNotificationMailer.new_article(u.id, a.id).deliver_now

I figured out how to fix this. 我想出了解决方法。

I added the code below to article.rb and added @article.send_notifications! 我将以下代码添加到article.rb,并添加了@ article.send_notifications! to my update controller. 到我的更新控制器。

def send_notifications!
 user = User.all
 user.each do |user|
   ArticleNotificationMailer.new_article(user, self).deliver_now
 end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM