2012-08-15 75 views
0

我的问题是,我没有想法创建一个用户列表的电子邮件,并在同一个列表中有一个复选框为每个用户,然后在最后有一个按钮'发送' 和发送的所有电子邮件的用户的检查,我解决了temporaly有: 1)名单(索引视图)复选框发送电子邮件红宝石轨道上

<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)发送控制器

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 

我使用标记“值”以检查电子邮件是否被发送

回答

1

我这样做对我的高级项目的一部分,这里是我是如何做的:

对于每个用户添加一个链接到每个用户的ID do循环内的复选框,看起来像:

<td><%= check_box_tag ':multiple_users[]', user.id %></td> 

添加do循环低于submit_tag,貌似,请注意,这个名字只是不同的动作来区分我们使用的时候,用户选择:

<%= submit_tag "Email Selected Users", :name => "selected" %> 

地方添加相应的表单标签在视图的顶部,矿去到处理程序,但你会去你的“发送”的动作,空哈希是很重要的:

<%= form_tag handle_checkbox_handler_path({}) do %> 

生成一个邮件,我没有这样做,我不知道怎么样,我敢肯定,谷歌确实:)你还需要为邮件制作一个配置文件(再次谷歌这一点,我不能帮你)。

一旦你的邮件,在其中创建一个动作以电子邮件用户:

def email_user(email, subject, text_body) 
    @text_body = text_body 
    mail(:to => email, :subject => subject, :from => "[email protected]") 
end 

一旦你的行动,使该电子邮件相应的视图(我的是非常简单,它只是放@text_body值。

<%= @text_body %> 

在你发送的行动将电子邮件发送给多个用户,这里是我的代码,我不是红宝石的专家,但它为我工作。此外,它不是很干净,所以我会为添加一些评论您的可读性:

如果不是params [':multiple_users']。nil? #有多个用户发邮件

# Split the passed string and loop through each ID in the string sending an email to each user listed 
    params[':users'].split.each do |u| 

     # Emailer is my mailer's name, :subject and :message I passed into the action as well. email_user is the name of my mailer action, defined above. 
     Emailer.email_user(User.find(u.to_i).email, params[:subject], params[:message]).deliver 
    end 
     redirect_to users_path, :notice => "Message was sent successfully" 

    else # there is only one user to email 
    if Emailer.email_user(params[:email], params[:subject], params[:message]).deliver # Try to send, if fails tell them so 
     redirect_to users_path, :notice => "Message was sent successfully" 
    else 
     redirect_to users_path, :alert => "Message failed to send" 
    end 
    end 
end 

我希望这是可读的,至少给你一个出发点。你也应该知道我前面提到的check_box_handler会做一些检查来确保没有什么不好的事情发生。

+0

谢谢你真的帮我!你的解释非常明确,特别,谢谢 – gaf 2012-08-16 17:27:43

相关问题