My problem is that I don’t have an idea to create a list of users with emails, and in the same list have a checkbox for every user, then at the end have a button ‘Send’ and send all the emails to the checked users, I solved temporaly with:
1)List (index view)
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Correo</th>
<th>Send Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% var1 = []; i = 0 %>
<% @users.each do |user| %>
<tr id=<%= if user.value == 'No dispatched'
'c'
elsif user.value == 'Dispatched'
'a'
else
'b'
end%> >
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.value %></td>
<td><% var1[i] = user.name; i=i+1 %>
<%= button_to 'Activate/Desactivate', edit_send_path(user.id), :method => :get %> </td>
</td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
<%= button_to 'Send', {:controller => :Send, :action => :send}, :method => :get, :value => 2 %>
2)Send controller
class SendController < ApplicationController
def send
@users = User.all
@users.each do |user|
if user.value == 'In Process'
UserMailer.registration_confirmation(user).deliver
user.value = 'Dispatched'
user.save
end
end
redirect_to :controller => :users, :protocol => 'http'
end
def edit
user = User.find(params[:id])
if user.value == 'In process'
user.value = 'No dispatched'
user.save
elsif user.value == 'No dispatched'
user.value = 'In process'
user.save
end
redirect_to :controller => :users, :protocol => 'http'
end
end
I’m using the flag ‘value’ to check if the email was sended
I did this for part of my senior project, here’s how I did it:
For each user add a checkbox that is linked to each user’s id inside the do loop, looks like:
Add a submit_tag below the do loop, looks like, note that the name was simply to differentiate between differing actions we used when selecting users:
Add the corresponding form tag somewhere at the top of the view, mine goes to the handler, but yours would go to your “send” action, the empty hash is important:
Generate a mailer, I did not do this and I do not know how, I’m sure Google does 🙂 You will also need to make a config file for the mailer (again google this, I can’t help you).
Once you have that mailer, create an action in it to email a user:
Once you have the action, make a corresponding view for the email (mine is very simple it just puts the
@text_bodyvalue.In your send action send the email to multiple users, here’s my code, I’m no Ruby expert but it worked for me. Also it’s not very clean so I’ll add some comments for your readability:
if not params[‘:multiple_users’].nil? # There are multiple users to email
I hope this is readable and at least gives you a starting point. You should also know that the check_box_handler I mentioned earlier does some checking to ensure that nothing bad happened.