I am working through the rails tutorial, and am on chapter 10 setting up mailers. Right now I am on the account_activation email. To ensure I am setting it up the correct way, I have even double checked my code based on the sample repo, and I am setting it up just like he has.
The full error is: ArgumentError in Rails::MailersController#preview wrong number of arguments (0 for 1) and is pointing to the code in my user_mailer#account_activation method
def account_activation(user) #here
#user = user
mail to: user.email, subject: "Account activation"
end
the test code this is checking against is this
class UserMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation
def account_activation
user = User.first
user.activation_token = User.new_token
UserMailer.account_activation(user) #setting up the method call here
end
end
I am not sure why this is happening, because I pass the correct number of arguments via UserMailer.account_activation(user). My only guess is maybe it could be looking at the UserMailerPreview#account_activation (which takes no argument) method and is trying to mirror the method at UserMailer#account_activation which does take an argument.
Does anyone see what I'm doing wrong? Any help at all would be greatly appreciated.
If you're calling class method and not instance method then it should be
def self.account_activation(user)
#user = user
mail to: user.email, subject: "Account activation"
end
and then
UserMailer.account_activation(user)
Related
I am trying to send mail to different users based on the user role of who is logged in on my site. I cannot seem to figure out a proper way to do it. I am using letter_opener gem to test on a dev environment, it seems this logic is not working. Can anyone please help me figure out how to make this logic work?
class ContactFormsMailer < ActionMailer::Base
default from: ENV["DEFAULT_FROM_EMAIL"]
def send_mail(contact_form)
#name = contact_form.name
#email = contact_form.email
#phone = contact_form.phone
#message = contact_form.body
#postcode = contact_form.postcode
if User.find_by_role('key_account')
mail(to: ENV["CONTACT_FORM_KAM"], subject: build_subject(contact_form))
else
mail(to: ENV["CONTACT_FORM_EMAIL"], subject: build_subject(contact_form))
end
end
def build_subject(contact_form)
if contact_form.body.include?("Calibration Certificate request:")
title = "CERTIFICATE REQUEST"
else
title = "#{ENV['COUNTRY_CODE']}: CONTACT FORM"
end
"#{title} - #{contact_form.email}"
end
end
All sorted. I just passed in the current user to the controller and used that on my mailer to check the currently logged-in user.
I am on ch11 of Hartl's rails tutorial, section 11.2.1 Mailer Templates, The preview for the password reset mailer works fine but the account_activation preview does not. the error actually changed even though i didn't make any changes to the code, which was confusing. However the error that I am currently receiving is
NoMethodError in Rails::Mailers#preview
Showing /home/ubuntu/workspace/sample_app/app/views/user_mailer/account_activation.html.erb where line #3 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #3)
<h1>Sample App</h1>
<p>Hi <%=#user.name %>,</p>
<p>
Welcome to the Sample App! Click on the link below to activate your account:
here is my user_mailer.rb
class UserMailer < ApplicationMailer
def account_activation(user)
#user = user
mail to: user.email, subject: "Account activation"
end
def account_activation
#greeting = "Hi"
mail to: "to#example.org"
end
def password_reset
#greeting = "Hi"
mail to: "to#example.org"
end
end
account_activations_controller.rb (no code has been specified so far in the tutorial)
class AccountActivationsController < ApplicationController
end
and the source of the error account_activation.html.erb
<h1>Sample App</h1>
<p>Hi <%=#user.name %>,</p>
<p>
Welcome to the Sample App! Click on the link below to activate your account:
</p>
<%= link_to "Activate", edit_account_activation_url(#user.activation_token,
email: #user.email) %>
it seems to not be recognising the name of the user. why could this be? (Note: all code is copied from the tutorial so i am at a loss as to the reason for the error)
thanks.
EDIT:As per the comment there wer indeed 2 account_activation methods. i have deleted the second one and kept the first. Now the error has changed to an argument error.
ArgumentError in Rails::MailersController#preview
wrong number of arguments (given 0, expected 1)
def account_activation(user)
#user = user
mail to: user.email, subject: "Account activation"
end
This method seems to be used in the following file without an argument.
/sample_app/test/mailers/previews/user_mailer_preview.rb
class UserMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation
def account_activation
user = User.first
user.activation_token = User.new_token
UserMailer.account_activation(user)
UserMailer.account_activation
end
# Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset
def password_reset
UserMailer.password_reset
end
end
In your UserMailer you currently have 2 versions of the account_activation method - one that takes a user parameter and a second one that doesn't. In Ruby you can't have 2 methods in a class with the same name so that second method will be replacing the earlier one.
I'm not familiar with with all the contents of this tutorial, but it seems likely the method without the parameter is from earlier in the tutorial and should have been removed now.
Then you will need to check the code where you're calling UserMailer.account_activation.deliver... and update it to be passing a user.
Update
In user_mailer_preview.rb you are calling account_activation twice:
...
UserMailer.account_activation(user)
UserMailer.account_activation
...
The call without user should be removed.
Controller
def mail_test
#user = User.all.first
#course = Course.all.first
examplemailer.student_reminder(#user, #course).deliver
redirect_to '/'
end
Mailer controller
def student_reminder(user, course)
#user = user
#course = course
#url = 'http://www.google.com'
mail to: #user.email, subject: "Good Job!"
end
Here, I need to use <%=#user.name%> and <%= #course.title%>.
However, that code doesn't work, ending in the error
ArgumentError in Rails::MailersController#preview
wrong number of arguments (1 for 2)
at
def student_reminder(user, course)
I think I have proper number of arguments (user and course)
As noted above, the error was in the MailerController#preview method.
I will preface this saying that I know almost nothing about rails. But I am trying to fix this issue involving rails so any help would be greatly appreciated (and also, if you could dumb it down for me that would be great!)
We have a rails email notification set up for two of our sites. If a user fills out an application on our English site, then a notification email is sent to person A. If a user fills out an application on our French site, then a notification email is sent to person B.
However, at the moment it seems like all emails are going to person A regardless of whether an application is filled out on the English or French site. How can I fix this?
Here is the code from the admin_mailer.rb file:
class AdminMailer < ActionMailer::Base
default from: 'noreply#canwise.com'
def contact_email
#contact = Contact.last
mail to: 'email1#1test.com'
end
def application_email
#application = Application.last
mail to: 'email1#test.com'
end
def eps_contact_email
#contact = Contact.last
mail to: "email2#test.com"
end
def eps_application_email
#application = Application.last
mail to: 'email2#test.com'
end
def salesforce_application_failure(application)
subject = "Application #{application.id} submitted by #{application.firstName} #{application.lastName} failed to submit to salesforce."
mail(to: 'test#test.com', subject: subject) do |format|
format.text { render text: '' }
end
end
end
And here is the code from the application.rb file:
def email_notification
if provider == 'www.frenchsite.com'
AdminMailer.eps_application_email.deliver
else
AdminMailer.application_email.deliver
end
end
HELP PLEASE!
If the emails are all going to email1#test.com, then it only means that your provider for french is not matching 'www.frenchsite.com'. The 'if' statement is always resulting in false.
i am using the devise for my User account where i will get all the confirmation, user password instructions etc;
But i am having a condition where if user account is locked (where status is false in my scenario), he has to get a reset password link through email. This locking process is coded in other controller so we cannot use devise helpers.
my controller code:
def send_instruction
a=[]
if #answer1
a << '1'
end
if #answer2
a << '2'
end
if #answer3
a << '3'
end
if a.size <= 1
SiteMailer.unlock_user(current_user).deliver
current_user.update_attribute(:status,false)
destroy_user_session_path(current_user)
flash[:error]= "Your account is locked"
redirect_to new_user_session_path
else
redirect_to user_dashboard_path
end
mailers/site_mailer.rb:
class SiteMailer < ActionMailer::Base
include Devise::Mailers::Helpers
default from: "support#example.com"
def unlock_user(user)
#user = user
#url = "pwd_edit"
mail(to: #user.email, subject: 'Your account has been locked')
end
end
in mailer view:
Hi,
Your account with has been locked. Edit Profile:
"My link (edit_user_password_url(current_user)".
When i am going through this process, I am getting "No routes found" error. And my routes are correct. Please help me out.
That's because no current_user is present at that time. You should try this instead
edit_user_password_url(#user)