简体   繁体   English

Ruby on Rails中的Mailer控制器,活动记录查询

[英]Controller for Mailer in Ruby on Rails, Active Record query

I followed a guide for setting up a mailer for ruby on rails (site of the guide: https://launchschool.com/blog/handling-emails-in-rails ). 我遵循了为在Rails上设置红宝石邮件的指南(该指南的站点: https : //launchschool.com/blog/handling-emails-in-rails )。

My preview looks like this: sample_email.html.erb 我的预览看起来像这样: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>

@user.username and @user.tag_list get rendered, but @infosall does not render. @ user.username和@ user.tag_list被渲染,但是@infosall不渲染。 Where do I need to type in @infosall, so it gets rendered on the preview? 我需要在哪里键入@infosall,以便它在预览中呈现?

example_mailer_preview.rb: example_mailer_preview.rb:

# 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

example_mailer.rb: example_mailer.rb:

class ExampleMailer < ApplicationMailer
  default from: ""

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

  end
end

users_controller.rb: users_controller.rb:

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

Edit: When I do this in example_mailer_preview.rb: 编辑:当我在example_mailer_preview.rb中这样做时:

# 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

I get a no method error: undefined method `find_first_mime_type' for # with this code: 我收到一个没有方法的错误:使用此代码的#的未定义方法`find_first_mime_type':

def find_preferred_part(*formats)
  formats.each do |format|
    if part = @email.find_first_mime_type(format)
      return part
    end
  end

Where did I go wrong? 我哪里做错了? Any advices? 有什么建议吗?

@infosall should be an instance variable of ExampleMailer that gets passed along to the template, but you are not setting this variable in the mailer. @infosall应该是ExampleMailer的实例变量,该实例变量将传递给模板,但是您未在邮件程序中设置此变量。 You would need to set it in your sample_email method on your ExampleMailer in order for it to have any value or meaning in your template. 您需要在ExampleMailer的sample_email方法中进行设置,以使其在模板中具有任何值或含义。

Mailer is not related to controller. 邮件程序与控制器无关。 Also defining a show action for some model does not get it called when you render something with that model. 同样,为某个模型定义show action时,使用该模型渲染某些东西时不会调用它。

Think of mailer as of a controller, set all needed instance variable in its actions ( sample_email in this case) 将邮件程序视为控制器,在其操作中设置所有需要的实例变量(在这种情况下为sample_email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM