0

我的问题是我不知道用电子邮件创建用户列表,并且在同一个列表中为每个用户都有一个复选框,然后最后有一个按钮“发送”并将所有电子邮件发送到选中用户,我暂时解决了: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

我正在使用标志“值”来检查电子邮件是否已发送

4

1 回答 1

1

我为我的高级项目的一部分做了这个,我是这样做的:

为每个用户添加一个复选框,该复选框链接到 do 循环内每个用户的 id,如下所示:

<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 => "monkey@feet.com")
end

完成操作后,为电子邮件创建相应的视图(我的非常简单,它只是放置@text_body值。

<%= @text_body %>

在您的发送操作中,将电子邮件发送给多个用户,这是我的代码,我不是 Ruby 专家,但它对我有用。而且它不是很干净,所以我会添加一些评论以提高您的可读性:

如果不是 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 会进行一些检查以确保没有发生任何不好的事情。

于 2012-08-16T01:31:57.823 回答