复选框在轨道上发送电子邮件红宝石 [英] Check box to send email ruby on rails

查看:111
本文介绍了复选框在轨道上发送电子邮件红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我没有一个想法,用电子邮件创建一个用户列表,在同一个列表中,每个用户都有一个复选框,最后还有一个按钮发送并发送所有的电子邮件给被检查的用户,我解决了暂时性:

1)列表(索引视图)

 <表> 
< tr>
< th> Name< / th>
< th>电子邮件< / th>
  Correo< / th>
< th>发送电子邮件< / th>
< th>< / th>
< th>< / th>
< th>< / th>
< / tr>

<%var1 = []; i = 0%>

<%@ users.each do | user | %GT;
< 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'显示',用户%>< / td>
< td><%= link_to'Edit',edit_user_path(user)%>< / td>
< td><%= link_to'Destroy',user,:method => :delete,:data => {:confirm => '你确定吗?'}%>< / td>
< / tr>
<%end%>
< / table>

< br />

<%= link_to'新用户',new_user_path%>


<%= button_to'发送',{:controller => :发送,:action => :send},:method => :get,:value => 2%>

2)发送控制器

  class SendController< ApplicationController 



def send
@users = User.all
@ users.each do | user |
如果user.value =='进程'
UserMailer.registration_confirmation(user).deliver
user.value ='已分派'
user.save
end
end
redirect_to:controller => :users,:protocol => 'http'
end

def edit
user = User.find(params [:id])
如果user.value =='进程'
user.value ='No dispatched'
user.save
elsif user.value =='No dispatched'
user.value ='进程'
user.save
end
redirect_to:controller => :users,:protocol => 'http'
end

end

我正在使用标志'值'来检查电子邮件是否被发送

解决方案

我是这样做的,我的部分高级项目,做到这一点:



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

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

在do循环下方添加一个submit_tag,看起来像,注意,这个名字只是为了区分不同的选择用户时使用的操作:

 <%= submit_tag发送选定的用户,:name => selected%> 

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

 <%= form_tag handle_checkbox_handler_path({}) %GT; 

生成邮件程序,我没有这样做,我不知道如何,我确定Google做:)你还需要为邮件程序配置一个配置文件(再次google这个,我不能帮你)。



一旦你有这个邮件程序,创建一个用于向用户发送电子邮件的动作:

  def email_user(email,subject,text_body)
@text_body = text_body
mail(:to => email,:subject => subject,:from =>monkey@feet.com)
end
/ pre>

一旦你有了这个动作,就可以看出电子邮件的相应视图(我的这个很简单,只是把 @text_body value。

 <%= @text_body%> 

在您的发送操作中,将电子邮件发送给多个用户,以下是我的代码,我不是Ruby专家,但它对我来说也是非常干净所以我会为您的可读性添加一些注释:



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

 #拆分传递的字符串,并循环遍历每个用户向每个用户发送电子邮件的每个ID列出
params [':users']。split.each do | u |

#电子邮件发件人是我的邮件人的姓名,主题和:消息我也传入了动作。 email_user是我的邮件操作的名称,定义如上。
Emailer.email_user(User.find(u.to_i).email,params [:subject],params [:message])。delivery
end
redirect_to users_path,:notice => 邮件已成功发送

else#只有一个用户发送电子邮件
如果Emailer.email_user(params [:email],params [:subject],params [:message]) .deliver#尝试发送,如果失败告诉他们所以
redirect_to users_path,:notice => 消息已成功发送
else
redirect_to users_path,:alert => 消息无法发送
end
end
end

我希望这是可读的,至少给你一个起点。你也应该知道,我之前提到的check_box_handler做了一些检查,以确保没有发生什么坏事。


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:

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

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:

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

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:

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

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:

def email_user(email, subject, text_body)
  @text_body = text_body
  mail( :to => email, :subject => subject, :from => "monkey@feet.com")
end

Once you have the action, make a corresponding view for the email (mine is very simple it just puts the @text_body value.

<%= @text_body %>

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

    # 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

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.

这篇关于复选框在轨道上发送电子邮件红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆