Beginner rails error我試圖發送電子郵件給所有當前用戶,當文章更新。Rails ActionMailer error undefined email
我有sendgrid和設計與我的應用程序設置,並能夠讓郵件工作通過rails控制檯。但是,由於某種原因,我在更新文章時收到undefined method email for #<User::ActiveRecord_Relation:0x007f8685aebec0>
。
ArticleNotificationMailer
class ArticleNotificationMailer < ApplicationMailer
default from: '[email protected]'
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>
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
錯誤消息
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