Question

I'm trying to do a diffusion (where you send an email to a lot of users). So in my model (with mongodb)

key :email, Array  

In my controller:

@users = params[:user]
@emails = Array.new
@users.each do |user|
  @emails << User.find_by_username(user).email
end

So in the link I pass as argument the array:

<%= link_to "Create a diffusion", mailer_path(:user => User.all)%>

In my form:

<%= f.input :email, :as => :hidden, :input_html => { :value => @emails } %>

The problem is in my mailer:

@message.email.each do |email|
  mail(:to => email, :subject => @message.subject)
end

The problem is that @message is not an array, is a string. So that loop will be an once-loop. I've tried also do @message.email.split(",") but doesn't work. I think it would be great if @message would be an array (how it should be)

Someone can help me? Thanks in advance

Was it helpful?

Solution 2

Finally I solved it! The problem was that I wasn't passing an array, the solution is in the form. the new form should be like:

<% @emails.each do |email| %>
  <%=f.input "email[#{email}]", :as => :text, :as => :hidden, :input_html => { :value => ""} %>
<% end %>

OTHER TIPS

Well, if you're naming is somehow reflecting the objects you are dealing with, maybe you should try:

@emails.each do |mail|
  mail(:to => mail, :subject => @message.subject, ...)
end

and why not:

User.all.each do |user|
  mail(:to => user, :subject => @message.subject, ...)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top