Reputation: 1093
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 ).
My preview looks like this: 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. Where do I need to type in @infosall, so it gets rendered on the preview?
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
Edit: When I do this in 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:
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?
Upvotes: 1
Views: 394
Reputation: 18504
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.
Think of mailer as of a controller, set all needed instance variable in its actions (sample_email
in this case)
Upvotes: 1
Reputation: 882
@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. 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.
Upvotes: 1