As a noob to rails I am struggling with being able to send a mail
template (html) stored in my database to all the users on a mailing
list. I can use the emailer page to send the mail template to a single
email address, but I want to be able to put a button on the mailinglists
show page that sends the mail template to all the mailings in the
mailinglist. The code is below for the key sections I am using.
I have been trying to figure out a solution for 4 days now and still
cannot get anything working and am loosing the will to live!!
Please if anyone could help me on this one I would be very very
greatfull.
As a noob to rails I am struggling with being able to send a mail
template (html) stored in my database to all the users on a mailing
list. I can use the emailer page to send the mail template to a single
email address, but I want to be able to put a button on the
mailinglists
show page that sends the mail template to all the mailings in the
mailinglist. The code is below for the key sections I am using.
At present the recipient is a single email address coming from the
emailer page (just to test the mailer works).
I have moved the sendmail form into the mailinglists index view page
(and also moved the sendmail function from the emailer controller to the
mailinglist controller) and it still works fine by sending the
recipient, subject, message to the emailer model.
However I created a mailings scaffold which holds each of the email
addresses, and names for the people on the mailinglist (with a one to
many relationship).
So now I am stuck as I dont know how to create a button that sends an
email to all the mailings in a mailinglist with the mailinglist_template
associated with that mailinglist!!
At present the recipient is a single email address coming from the
emailer page (just to test the mailer works).
I have moved the sendmail form into the mailinglists index view page
(and also moved the sendmail function from the emailer controller to
the
mailinglist controller) and it still works fine by sending the
recipient, subject, message to the emailer model.
However I created a mailings scaffold which holds each of the email
addresses, and names for the people on the mailinglist (with a one to
many relationship).
So now I am stuck as I dont know how to create a button that sends an
email to all the mailings in a mailinglist with the
mailinglist_template
associated with that mailinglist!!
Doesn't that just mean that recipients = mailinglist.people.collect {|
person> person.email_address}
?
Fred
However I created a mailings scaffold which holds each of the email
addresses, and names for the people on the mailinglist (with a one to
many relationship).
So now I am stuck as I dont know how to create a button that sends an
email to all the mailings in a mailinglist with the
mailinglist_template
associated with that mailinglist!!
Doesn't that just mean that recipients = mailinglist.people.collect {|
person> person.email_address}
?
Fred
However I created a mailings scaffold which holds each of the email
addresses, and names for the people on the mailinglist (with a
one to
many relationship).
So now I am stuck as I dont know how to create a button that
sends an
email to all the mailings in a mailinglist with the
mailinglist_template
associated with that mailinglist!!
Doesn't that just mean that recipients =
mailinglist.people.collect {|
person> person.email_address}
?
Fred
so does that mean I put an AJAX button in my view page to run the "do"
code, or does the code have to be placed in the emailer model?
whether this happens in the controller or in the emailer this mostly
depends if you want 1 email sent to everyone, or to send however many
separate emails (with identical content).
I think AJAX is an orthogonal issue - you could have a regular button
or an ajax one and it wouldn't change much.
I think AJAX is an orthogonal issue - you could have a regular button
or an ajax one and it wouldn't change much.
Fred
to be honest i dont really care how the emails are sent. which ever
method is easiest to impliment. Do you know of any examples that are
similar as I dont really know where to start on this one.
I'm sort of struggling to see what the problem is to be honest.
I think AJAX is an orthogonal issue - you could have a regular
button
or an ajax one and it wouldn't change much.
Fred
to be honest i dont really care how the emails are sent. which ever
method is easiest to impliment. Do you know of any examples that are
similar as I dont really know where to start on this one.
I'm sort of struggling to see what the problem is to be honest.
This line above is pointless. collect with no block passed just
returns the original array.
THe point of collect/map is to create a new collection by applying a
function to each element (eg given an array of numbers construct an
array with the square of each number: numbers.collect {|n| n*n} )
@mailing_list.mailings.each do |mailing|
Emailer.deliver_contact(mailing.email_address, @mail_template.subject, @mail_template.body)
end
This line above is pointless. collect with no block passed just
returns the original array.
THe point of collect/map is to create a new collection by applying a
function to each element (eg given an array of numbers construct an
array with the square of each number: numbers.collect {|n| n*n} )
@mailing_list.mailings.each do |mailing|
Emailer.deliver_contact(mailing.email_address,
@mail_template.subject, @mail_template.body)
end
Fred
one final question.. i have the code working, and can send a mail
template to a list of mailings... but in order to send more than one
mailtemplate to these users i have had to create a button for each
template as shown below..
<% for mail_template in @mailinglist_templates %>
<%= button_to "sendmail", { :action => "send_mail", :mail_template_id =>
mail_template.id, :id => params[:id] } %>
<% end %>
now this i know is poor programming and looks bad too.. is there a way i
can just have one button that sends each of the mail templates to all
the users in the list..?
one final question.. i have the code working, and can send a mail
template to a list of mailings... but in order to send more than one
mailtemplate to these users i have had to create a button for each
template as shown below..
<% for mail_template in @mailinglist_templates %>
<%= button_to "sendmail", { :action => "send_mail", :mail_template_id =>
mail_template.id, :id => params[:id] } %>
<% end %>
now this i know is poor programming and looks bad too.. is there a way i
can just have one button that sends each of the mail templates to all
the users in the list..?
just call the action something like send_all_mails or something like
that and drop the :mail_template_id parameter and do that iteration
over all templates in the controller.
If you wanted to get a bit more fancy then you could have a checkbox
for each template you want to send (whether you have a list of
templates with a checkbox for each mailing list that should receive it
or a list of mailing list with checkboxes for each template that
should go to a given mailing list is down to what makes the most
business sense to you)
now this i know is poor programming and looks bad too.. is there a way i
can just have one button that sends each of the mail templates to all
the users in the list..?
just call the action something like send_all_mails or something like
that and drop the :mail_template_id parameter and do that iteration
over all templates in the controller.
If you wanted to get a bit more fancy then you could have a checkbox
for each template you want to send (whether you have a list of
templates with a checkbox for each mailing list that should receive it
or a list of mailing list with checkboxes for each template that
should go to a given mailing list is down to what makes the most
business sense to you)
@mail_templates.mail_templates.each do |mail_template|
@mailing_list.mailings.each do |mailing|
Emailer.deliver_contact(mailing.email_address,
@mail_template.name, @mail_template.content)
end
end
>> now this i know is poor programming and looks bad too.. is there a way i
>> can just have one button that sends each of the mail templates to all
>> the users in the list..?
> just call the action something like send_all_mails or something like
> that and drop the :mail_template_id parameter and do that iteration
> over all templates in the controller.
> If you wanted to get a bit more fancy then you could have a checkbox
> for each template you want to send (whether you have a list of
> templates with a checkbox for each mailing list that should receive it
> or a list of mailing list with checkboxes for each template that
> should go to a given mailing list is down to what makes the most
> business sense to you)
� � @mail_templates.mail_templates.each do |mail_template|
Just @mailing_list.mail_templates.each ...
will do here, assuming mailing_list has_many mail_templates
Fred
brilliant, thank you Fred. I have the code working without syntax
errors. There is however a problem where it will only work once in both
loops. for example if i have more than one email address in the
mailinglist, or more than one template it sends the first email to me no
problem but then throws the following error
ArgumentError in Admin/mailinglistsController#send_mail
Admin is not missing constant Emailer!
RAILS_ROOT: C:/ruby/myprojects/service
{"authenticity_token"=>"dc9249eaed248ed10c939b4b0fd52c0c373c5f09",
"id"=>"1"}
Show session dump
dont worry, i fixed it by prepending Admin:: to the reference of the
model.. but out of curiosity, why did it work the first time and not the
second time?
> Application Trace | Framework Trace | Full Trace
dont worry, i fixed it by prepending Admin:: to the reference of the
model.. but out of curiosity, why did it work the first time and not the
second time?
Probably some weird dependencies thing. Hard to say more from here.