我正在尝试设置邮件程序。当用户向讨论提交评论时,我希望该讨论的所有者接收电子邮件,但我一直收到NoMethodError。我无法弄清楚我应该在哪里定义email
......我希望这是我唯一的问题。
undefined method 'email' for #<ActiveRecord::Associations::CollectionProxy []>
在这一行:mail to: @user.email, subject: "You've got a comment!"
comments_controller.rb创建功能:
def create
@discussion = Discussion.find(params[:discussion_id])
@comment = @discussion.comments.build(comment_params)
if @comment.save
Usermailer.commentcreated_email(@comment).deliver
redirect_to new_discussion_comment_path(@discussion)
end
end
usermailer.rb
class Usermailer < ApplicationMailer
default from: "***took out***"
def commentcreated_email(comment)
@comment = comment
@user = @comment.users
mail to: @user.email, subject: "You've got a comment!"
end
end
development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:adress => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "***I do have an actual email here***",
:password => "***And an actual password***",
:authentication => "plain",
:enable_starttls_auto => true }
commentcreated_email.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Comment Created</h1>
<p>User <%= @comment.user %> posted a comment: </p>
<br> <%= @comment.created_at.strftime('%m/%d/%Y %I:%M %p') %>
<br> Project: <%= @comment.project.name %>
<br> Hours: <%= @comment.hours %>
</body>
</html>
现在我没有在我的user.rb中添加任何东西,但也许我应该有?这就是它的样子:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :likes, dependent: :nullify
has_many :liked_comments, through: :likes, source: :comment
def has_liked?(comment)
liked_comments.include? comment
end
end
答案 0 :(得分:0)
您正在使用@user = @comment.users
,它会返回一个数组<ActiveRecord::Associations::CollectionProxy []>
。因此,在阵列上使用电子邮件总会引发错误。更改它将修复错误。