2012-08-15 73 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

相關問題