ActionMailer pass local variables to the erb template
All options available in the mail
method can be found at http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail.
We know that render
has a :locals
option. However we can see that there is no :locals
option available for mail
. Therefore, no, there is no better way than to use instance variables (unless you want to use something hideous like globals or persistent database objects - don't do this).
Instance variables are what you are meant to use.
Rails - Mailer Instance Variable Nil in Email View
try this way
This is your InvitationMailer
def invitation(invitation)
@invitation = invitation
mail(:to => @invitation.recipient_email, :subject => "Invitation")
end
now, in your InvitationsController
if signed_in?
@invitation.update_attribute(:sent_at, Time.now)
InvitationMailer.invitation(@invitation).deliver
...
else
...
end
now, views/invitation_mailer/invitation.text.erb
You are invited to join the site!
<%= invited_url(@invitation.token) %> # INSTANCE VARIABLE THAT IS NIL IN VIEW
Passing Variable into Rails Mailer
Well, you are using local variable user
, which has scope only to the method you have defined. You have to use instance variable @user
as an example. So changes are to be done like :
def intro_email(user)
@user = user
mail(to: "email", from: "email2", subject: "THIS IS A TEST")
end
Then inside the view use @user
. LikeThe users name is <%= @user.name %>.
Send email with attachments action Mailer Ruby on Rails
# controller
InvoiceMailer.send_invoice(attachment).deliver
# mailer
def send_invoice(attachment)
attachments['test-bill'] = File.read(attachment)
...
end
Rails 5 render set instance variables in locals
ActionView::Base
has a method assign which can be called to set the instance variables.
av.assign({revenue_accounts: revenue_accounts,
expense_accounts: expense_accounts,
start_date: start_date,
end_date: end_date,
business: business})
income_statement_html = av.render :template => "reports/income_statement.pdf.erb", :layout => 'layouts/report.html.erb'
Undefined local variable or method `params' - In Mailer
ActionMailer does not have access to the controller params
, so you can't access params
hash inside your mailer.
You can pass the params you need as arguments instead like you passed the user
in the argument in your send_notification_email
method. Use that user's id if you need:
def send_notification_email(user)
user = User.friendly.find(user.id) # use the id of the passed user
mail( :to => user.email,
:subject => "You've got a post like" )
end
Apparently, you can even omit user = User.friendly.find(user.id) # use the id of the passed user
line because you already have the user
object which you can directly use in the mail
method call:def send_notification_email(user)
mail(:to => user.email,
:subject => "You've got a post like")
end
Both of these should work. And the bottom line is: ActionMailer does not have access to the controller params
hash which is the cause of your error. :-) Passing a block variable in an js.erb ajax call
views/dishes/like.js.erb
$(".restaurant__dish").html('<%= escape_javascript(render "restaurants/dish_partials/like_toggle", dish: @dish) %>');
or$(".restaurant__dish").html('<%= escape_javascript(render partial: "restaurants/dish_partials/like_toggle", locals: { dish: @dish }) %>');
you're missing @ before dish variable.
Related Topics
Ruby 1.9 How to Convert Array to String Without Brackets
Capybara & Cucumber | Getting Cookies
Paperclip and Amazon S3 How to Do Paths
Server Sent Events and Rails Streaming
When Is The Enumerator::Yielder#Yield Method Useful
What Are Ruby's Numbered Global Variables
Difference Between Render and Yield in Rails
Can Sunspot Search Inside Array
How to "Break" Out of a Case...While in Ruby
Sqlite3::Sqlexception: Duplicate Column Name While Migrating
Why Should @@Class_Variables Be Avoided in Ruby
Will_Paginate Find Out If I'M on The Last Page
Can't Install Debugger Gem - Rails - MAC Osx Mavericks
How to Test Helpers Blocks in Sinatra, Using Rspec
Set Ruby 2.0 Keyword Arguments with Attr_Accessor on Initialize