Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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 RubyonRails中邮件程序的控制器,活动记录查询_Ruby On Rails_Mailer - Fatal编程技术网

Ruby on rails RubyonRails中邮件程序的控制器,活动记录查询

Ruby on rails RubyonRails中邮件程序的控制器,活动记录查询,ruby-on-rails,mailer,Ruby On Rails,Mailer,我遵循了为RubyonRails(指南网站:)设置邮件程序的指南 我的预览如下所示:sample_email.html.erb <!DOCTYPE html> <html> <head> <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> </head> <body> <h1>Hi <%= @user.usernam

我遵循了为RubyonRails(指南网站:)设置邮件程序的指南

我的预览如下所示:sample_email.html.erb

<!DOCTYPE html>
<html>
<head>
  <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Hi <%= @user.username %></h1>
<p>
 Sie haben folgende Tags ausgewählt:
  <% @user.tag_list.each do |tag| %>
    <%= tag %> <br>
  <% end %>

  <br><br>
  <% @infosall.each do |info| %> #<-- Problem on this line
      <%= info.name %><br>
  <% end %><br>
</p>
</body>
</html>

我哪里出错了?有什么建议吗?

@infosall
应该是传递给模板的ExampleMailer的实例变量,但您不能在邮件器中设置此变量。您需要在ExampleMailer上的
sample\u email
方法中设置它,以便它在模板中具有任何值或意义

邮件程序与控制器无关。在使用某个模型渲染某个对象时,为该模型定义
show
操作也不会调用它


将mailer视为一个控制器,在其操作中设置所有需要的实例变量(
sample\u email
在本例中)

我在sample\u email中添加了一些内容,但我收到了一个错误(请参见我的编辑),有什么建议吗?@Metaphysiker您在预览中添加了代码,而不是mailer本身,预览应该返回一个邮件对象我在sample\u email中添加了一些内容,但是我得到了一个错误(见我的编辑),有什么建议吗?你把它添加到了
sample\u email\u preview
,而不是
sample\u email
。至于您提到的错误,它存在于一个方法中,您甚至没有从任何发布的代码中调用它,因此我不确定它与OP的关系。
# Preview all emails at http://localhost:3000/rails/mailers/example_mailer
class ExampleMailerPreview < ActionMailer::Preview
  def sample_mail_preview
    ExampleMailer.sample_email(User.last)
  end
end
class ExampleMailer < ApplicationMailer
  default from: ""

  def sample_email(user)
    @user = user
    mail(to: @user.email, subject: 'Sample Email')

  end
end
class UsersController < ApplicationController

  def show
    @user = User.find_by_username(params[:username])
    @tags = @user.tag_list
    @infosall = Array.new
    @tags.each do |tag|
      @infosall = @infosall + Info.tagged_with(tag)
    end

    @infosall.uniq!
    @infosall.sort! { |a,b| b.created_at <=> a.created_at }
  end

end
# Preview all emails at http://localhost:3000/rails/mailers/example_mailer
class ExampleMailerPreview < ActionMailer::Preview
  def sample_mail_preview
    ExampleMailer.sample_email(User.last)

        @user = User.last
        @tags = @user.tag_list
        @infosall = Array.new
        @tags.each do |tag|
          @infosall = @infosall + Info.tagged_with(tag)
        end

        @infosall.uniq!
        @infosall.sort! { |a,b| b.created_at <=> a.created_at }
  end
end
def find_preferred_part(*formats)
  formats.each do |format|
    if part = @email.find_first_mime_type(format)
      return part
    end
  end