Rails ActionMailer 错误未定义电子邮件
Rails ActionMailer error undefined email
初学者 rails 错误 我正在尝试在文章更新时向所有当前用户发送电子邮件。
我已经使用我的应用程序设置了 sendgrid 和设计,并且能够让邮件程序通过 rails 控制台工作。但是,出于某种原因,我在更新文章时收到 undefined method email for #<User::ActiveRecord_Relation:0x007f8685aebec0>
。
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
<!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>
文章控制器
我正在使用 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
错误信息
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 控制台
>> u = User.last
>> a = Article.first
>> ActionNotificationMailer.new_article(u, a).deliver_now
ArticleNotificationMailer.new_article(@user, @article).deliver
似乎 @user 在您的控制器中由 User.where() 初始化。 User.where returns User::ActiveRecord_Relation 的实例实际上是 rails - 增强数组。当您尝试在此数组上调用 email 时出现错误。
如果您只需要查找一条记录,只需使用User.find。
尝试传入元素的 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
我想出了解决办法。
我在article.rb中添加了下面的代码,并添加了@article.send_notifications!到我的更新控制器。
def send_notifications!
user = User.all
user.each do |user|
ArticleNotificationMailer.new_article(user, self).deliver_now
end
end
初学者 rails 错误 我正在尝试在文章更新时向所有当前用户发送电子邮件。
我已经使用我的应用程序设置了 sendgrid 和设计,并且能够让邮件程序通过 rails 控制台工作。但是,出于某种原因,我在更新文章时收到 undefined method email for #<User::ActiveRecord_Relation:0x007f8685aebec0>
。
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
<!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>
文章控制器
我正在使用 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
错误信息
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 控制台
>> u = User.last
>> a = Article.first
>> ActionNotificationMailer.new_article(u, a).deliver_now
ArticleNotificationMailer.new_article(@user, @article).deliver
似乎 @user 在您的控制器中由 User.where() 初始化。 User.where returns User::ActiveRecord_Relation 的实例实际上是 rails - 增强数组。当您尝试在此数组上调用 email 时出现错误。
如果您只需要查找一条记录,只需使用User.find。
尝试传入元素的 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
我想出了解决办法。
我在article.rb中添加了下面的代码,并添加了@article.send_notifications!到我的更新控制器。
def send_notifications!
user = User.all
user.each do |user|
ArticleNotificationMailer.new_article(user, self).deliver_now
end
end