Ruby Rails 中的 Mailer 控制器,Active Record 查询
Controller for Mailer in Ruby on Rails, Active Record query
我在 rails 上遵循了为 ruby 设置邮件程序的指南(指南站点:https://launchschool.com/blog/handling-emails-in-rails)。
我的预览是这样的: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 和@user.tag_list 得到渲染,但@infosall 不渲染。我需要在哪里输入@infosall,以便它在预览中呈现?
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:
class ExampleMailer < ApplicationMailer
default from: ""
def sample_email(user)
@user = user
mail(to: @user.email, subject: 'Sample Email')
end
end
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
编辑:当我在 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
我收到一个无方法错误:未定义方法 `find_first_mime_type' for # with this code:
def find_preferred_part(*formats)
formats.each do |format|
if part = @email.find_first_mime_type(format)
return part
end
end
我哪里错了?有什么建议吗?
@infosall
应该是传递给模板的 ExampleMailer 的实例变量,但您没有在邮件程序中设置此变量。您需要在 ExampleMailer 的 sample_email
方法中设置它,以便它在您的模板中具有任何值或意义。
Mailer 与控制器无关。还为某些模型定义了一个 show
操作,当您使用该模型渲染某些内容时,它不会被调用。
将邮件程序视为控制器,在其操作中设置所有需要的实例变量(在本例中为sample_email
)
我在 rails 上遵循了为 ruby 设置邮件程序的指南(指南站点:https://launchschool.com/blog/handling-emails-in-rails)。
我的预览是这样的: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 和@user.tag_list 得到渲染,但@infosall 不渲染。我需要在哪里输入@infosall,以便它在预览中呈现?
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:
class ExampleMailer < ApplicationMailer
default from: ""
def sample_email(user)
@user = user
mail(to: @user.email, subject: 'Sample Email')
end
end
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
编辑:当我在 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
我收到一个无方法错误:未定义方法 `find_first_mime_type' for # with this code:
def find_preferred_part(*formats)
formats.each do |format|
if part = @email.find_first_mime_type(format)
return part
end
end
我哪里错了?有什么建议吗?
@infosall
应该是传递给模板的 ExampleMailer 的实例变量,但您没有在邮件程序中设置此变量。您需要在 ExampleMailer 的 sample_email
方法中设置它,以便它在您的模板中具有任何值或意义。
Mailer 与控制器无关。还为某些模型定义了一个 show
操作,当您使用该模型渲染某些内容时,它不会被调用。
将邮件程序视为控制器,在其操作中设置所有需要的实例变量(在本例中为sample_email
)