Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:NoMethodError在尝试设置邮件程序时_Ruby On Rails_Ruby_Email_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails:NoMethodError在尝试设置邮件程序时

Ruby on rails Rails:NoMethodError在尝试设置邮件程序时,ruby-on-rails,ruby,email,ruby-on-rails-4,Ruby On Rails,Ruby,Email,Ruby On Rails 4,我正试着建立一个邮件发送系统。当用户向讨论提交评论时,我希望该讨论的所有者收到一封电子邮件,但我总是收到一封匿名邮件。我不知道我应该在哪里定义电子邮件。。。我希望这是我唯一的问题 未定义的方法“email”用于# 在这里的这一行:发送至:@user.email,主题:“你有评论!” 注释\u controller.rb创建函数: def create @discussion = Discussion.find(params[:discussion_id]) @comment

我正试着建立一个邮件发送系统。当用户向讨论提交评论时,我希望该讨论的所有者收到一封电子邮件,但我总是收到一封匿名邮件。我不知道我应该在哪里定义
电子邮件
。。。我希望这是我唯一的问题

未定义的方法“email”用于#

在这里的这一行:
发送至:@user.email,主题:“你有评论!”

注释\u 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
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
class用户
您正在使用
@user=@comment.users
,它将返回一个数组
。因此,在阵列上使用电子邮件总是会引发错误。更改它错误将被修复。

谢谢,我刚刚将其更改为.user,但现在我遇到了一个以前从未见过的奇怪错误
uninitialized constant ApplicationMailer
并在my Usermailer.rb?rogerat中突出显示
class Usermailer
,如果您没有在第一次运行
rails g mailer MailerName
时创建的
ApplicationMailer
@D-side hm,所以我继续运行了
railsgmailer-usermailer
,因为我已经有了它,所以它跳过了创建所有这些。除此之外,由于某种原因,我显然没有
应用程序\u mailer.rb
,所以它确实创建了它。这修正了那个错误。但现在我又回到了一个未定义的方法错误<代码>未定义的#
@roguerat
注释的方法“user”属于:user
或not?@D-side-Ah似乎很有帮助!我修正了它,它稍微改变了错误,现在我几乎回到了我原来的错误,只是有点不同<代码>未定义的方法email'for nil:NilClass`所以我假设这意味着email在任何地方都没有定义?
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