2016-08-03 15 views
0

私は電子メールを送信するためにRoRにシステムを作成しようとしています。ユーザーができるようにしたいのは、フォーム上のチェックボックスで複数の電子メールアドレスを選択し、それぞれに。分でユーザーはフォーム上の複数の電子メールアドレスを選択することができ、システムはそれらを一緒に結合するので、1つの電子メールが複数の受信者に送信されます。ユーザは電子メールアドレス「1aa.com」および「2b.com」を選択し、システムは「1a.com; 2b.com」に電子メールを送信する。Ruby on Rails:電子メールアドレスのチェックボックスをループし、それぞれに電子メールを送信しますか?

私は、ビューを介してこれを行っている:

<%= form_for(@email) do |f| %> 
    <% if @email.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@email.errors.count, "error") %> prohibited this email from being saved:</h2> 

     <ul> 
     <% @email.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <p> 
    <%= f.label :from_name %><br> 
    <% if @user.present? %> 
    <%= f.select :from_name, [@user.firstname + " " + @user.surname, @user.organisation] %> 
    <% end %> 
    </p> 

    <p> 
    <%= f.label :From_Email_Address %><br> 
    <%= f.collection_select :account_id, Account.where(user_id: session[:user_id]), 
     :id,:email %> 
    </p> 

    <p> 
    <%= f.label :to %><br> 
    <%= f.collection_check_boxes(:to, @contacts, :email, :email) %> 
    </p> 

    <p> 
    <%= f.label :cc %><br> 
    <%= f.text_field :cc %> 
    </p> 

    <p> 
    <%= f.label :bcc %><br> 
    <%= f.text_field :bcc %> 
    </p> 

    <p> 
    <%= f.label :subject %><br> 
    <%= f.text_field :subject %> 
    </p> 

    <p> 
    <%= f.label :message %><br> 
    <%= f.text_field :message %> 
    </p> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 

連絡先データベースのテーブルに保存された電子メールアドレスごとにチェックボックスを示す線<%= f.collection_check_boxes(:to, @contacts, :email, :email) %>で。

これは、次のコードがあるemails_controllerに通過します:一緒に選択したメールアドレスに参加email_paramsコードで

class EmailsController < ApplicationController 
    before_action :set_email, only: [:show, :edit, :update, :destroy] 

    def index 
    # Only show the email accounts for the user logged in 
    @emails = Email.where(user_id: session[:user_id]) 
    end 

    def show 
    end 

    def new 
    @user = User.find(session[:user_id]) 
    @contacts = Contact.where(user_id: session[:user_id]) 
    @email = Email.new 
    end 

    def edit 
    @user = User.find(session[:user_id]) 
    @contacts = Contact.where(user_id: session[:user_id]) 
    end 

    def create 
    @email = Email.new(email_params) 
    @email.user_id = session[:user_id] 
    @user = session[:user_id] 
    if @email.save 
     UserEmails.send_email(@email).deliver_now 
     redirect_to @email, notice: 'Email was successfully created.' 
    else 
     redirect 'new' 
    end 
    end 

    def update 
     if @email.update(email_params) 
     redirect_to @email, notice: 'Email was successfully updated.' 
     else 
     redirect 'edit' 
     end 
    end 

    def destroy 
    @email.destroy 
    redirect_to emails_url, notice: 'Email was successfully destroyed.' 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_email 
      @email = Email.find(params[:id]) 
end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def email_params 
     res = params.require(:email).permit(:account_id, :cc, :bcc, :subject, :message, :from_name, to: []) 
     res[:to] = res[:to].join('; ') 
     res 
    end 
end 

を「;」それぞれの間に

createの方法のように、UserEmails.send_email(@email).deliver_nowコードは電子メールが保存されると実行されます。このコードは電子メールを送信します。

私の質問は、フォーム上で選択された電子メールアドレスごとに1つの電子メールが送信されるように誰でもコードを変更できるようにすることができるかどうかです。

以下は、私のフォームのチェックボックスの様子です。

enter image description here

答えて

0

使用する複数の選択ボックスと、その次のコードは、

フォーム

<div class="form-group"> 
 
       <%= f.label :select_user_email %><br/> 
 
       <select class="form-control select2" name="user[email][]"> 
 
       <option value="" selected disabled>Enter email address</option> 
 
       <%@users.each do |user|%> 
 
        <option value="<%=user.email%>"><%=user.name%></option> 
 
       <%end%> 
 
       </select> 
 
      </div>

に動作した後、グーグルが行うよう、メールアドレスの選択ボックスを作るためSELECT2 SELECT2を覚えておいてください
def create 
    @emails = (params[:email]).split(",") 
    @emails.each do |email| 
    UserMailer(email, @message).deliver_now 
end 
end 

を使用すると、各電子メールアドレスのチェックボックスを追加することを意味しません、あなたは「複数選択ボックスを使用する」と言うこの enter image description here

+0

こんにちはプラディープ、同様に選択ボックスを作成するためのJavaScriptとCSSです'collection_check_boxes'を使うのではなく? –

+0

編集していただきありがとうございますが、選択した連絡先のメールアドレスは表示されません。私。私が見ることができる唯一の価値は、「Eメールアドレスの入力」です。何らかの理由でEメールが引き抜かれていません。どんな考え? –

+0

チェックボックスを使用してこれを行う方法はありますか? –

関連する問題