Rails4:群发短信攻略——传递多条记录给一个控制器

Rails 4: Group Text Message Strategy - Passing Multiple Records to a Controller

我的 Rails 应用程序让用户创建客户端,然后使用 Twilio 发送或安排 text_messages。创建一个客户端然后生成一个 text_message 记录效果很好。用户拥有客户,客户拥有 Text_Message。

我要解决的下一个功能是允许用户选择他们也想向哪些客户发送群发短信。我想为每个客户生成一条 text_message 记录(具有相同的内容和 scheduled_date)。已保存 text_messages 正在添加到 Sidekiq 队列中。

我需要一些关于此策略的帮助(请)。如何在保持代码干燥并继续为 text_message table 生成记录的同时实现 Group_Text?如何在使用 Text_Message 模型的逻辑时将多个选定的客户端从 form/view 传递到控制器以创建记录?谢谢指点!

相关代码

schema.rb

    ActiveRecord::Schema.define(version: 20141207214913) do

      # These are extensions that must be enabled in order to support this database
      enable_extension "plpgsql"

      create_table "action_plans", force: true do |t|
        t.integer  "client_id"
        t.text     "description"
        t.datetime "created_at"
        t.datetime "updated_at"
      end

      add_index "action_plans", ["client_id"], name: "index_action_plans_on_client_id", using: :btree

      create_table "clients", force: true do |t|
        t.integer  "user_id"
        t.string   "first_name",    null: false
        t.string   "last_name",     null: false
        t.string   "phone",         null: false
        t.string   "salesforce_id", null: false
        t.string   "email"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.text     "contact_id"
      end

      add_index "clients", ["user_id"], name: "index_clients_on_user_id", using: :btree

      create_table "coach_emails", force: true do |t|
        t.integer  "user_id"
        t.string   "email",            null: false
        t.string   "coach_firstname"
        t.string   "client_lastname"
        t.string   "client_firstname"
        t.text     "content",          null: false
        t.boolean  "sentstatus"
        t.datetime "created_at"
        t.datetime "updated_at"
      end

      add_index "coach_emails", ["user_id"], name: "index_coach_emails_on_user_id", using: :btree

      create_table "goals", force: true do |t|
        t.integer  "action_plan_id"
        t.text     "description"
        t.datetime "created_at"
        t.datetime "updated_at"
      end

      add_index "goals", ["action_plan_id"], name: "index_goals_on_action_plan_id", using: :btree

      create_table "steps", force: true do |t|
        t.integer  "goal_id"
        t.text     "description"
        t.date     "due_by"
        t.boolean  "complete",    default: false
        t.datetime "created_at"
        t.datetime "updated_at"
      end

      add_index "steps", ["goal_id"], name: "index_steps_on_goal_id", using: :btree

      create_table "text_messages", force: true do |t|
        t.integer  "client_id"
        t.text     "content"
        t.boolean  "incoming_message"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.date     "scheduled_date"
        t.boolean  "sentstatus"
        t.integer  "step_id"
        t.string   "phone"
      end

      add_index "text_messages", ["client_id"], name: "index_text_messages_on_client_id", using: :btree

      create_table "users", force: true do |t|
        t.string   "email",                  default: "", null: false
        t.string   "encrypted_password",     default: "", null: false
        t.string   "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.integer  "sign_in_count",          default: 0,  null: false
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.inet     "current_sign_in_ip"
        t.inet     "last_sign_in_ip"
        t.string   "first_name"
        t.string   "last_name"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.string   "role"
        t.string   "title"
        t.string   "confirmation_token"
        t.datetime "confirmed_at"
        t.datetime "confirmation_sent_at"
        t.string   "unconfirmed_email"
      end

      add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
      add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

    end

text_message.rb

    require 'twilio-ruby'
    require 'date'

    class TextMessage < ActiveRecord::Base

      belongs_to :client, dependent: :destroy
      belongs_to :step, dependent: :destroy
      has_many :coach_emails

    before_save :grab_phone

      def grab_phone
        self.phone = phone
      end

      def send_text_message(message, phone)

        twilio_sid = ENV["TWILIO_ACCT_SID"]
        twilio_token = ENV["TWILIO_AUTH_TOKEN"]
        twilio_phone_number = ENV["TWILIO_PHONE_NUMBER"]

        begin
          @twilio_client = Twilio::REST::Client.new(twilio_sid, twilio_token)

          @twilio_client.account.sms.messages.create(
            :from => "+1#{twilio_phone_number}",
            :to => phone,
            :body => message)

          rescue Twilio::REST::RequestError => e
            puts e.message
        end

        if e != "400" || e != "500"
          self.sentstatus = true
        end

        self.save!
      end  
    end

routes.rb

    Rails.application.routes.draw do

    require 'sidekiq/web'

      devise_for :users, :path => '',
        :path_names => {:sign_in => 'login', :sign_out => 'logout'}, 
        :controllers => { registrations: 'registrations' }

      authenticate :user do
        mount Sidekiq::Web => '/sidekiq'
      end

      resources :clients do
        resources :action_plans, shallow: true, except: [:index] 
      end

      resources :action_plans, shallow: true, only: [] do
        resources :goals, shallow: true, except: [:index]
      end

      resources :goals, shallow: true, only: [] do
        resources :steps, shallow: true, except: [:index, :destroy]
      end

      resources :steps, shallow: true, only: [] do
        resources :text_messages, shallow: true, except: [:index, :destroy]
      end

      get "text_messages/receive"
      match '/receivetext' => 'text_messages#receive', :via => :post

      resources :clients do
        collection do
          post :welcome
        end
      end

      get 'about' => 'welcome#about'

      root to: 'welcome#index'

    end

我发现我需要添加到 TextMessage 控制器(#groupnew 和 #groupcreate)。

新路线

get 'group_new' => 'text_messages#group_new' 
post 'group_create' => 'text_messages#group_create'

更新了 TextMessage 控制器

def group_new
  @clients = current_user.clients.order(:last_name)
  @text_message = TextMessage.new 
end

def group_create
  client_hash = params[:client_id]

  client_hash.each do |client|
    @client = Client.find(client)
    content = params[:text_message][:content]

    @text_message = @client.text_messages.build(text_message_params)
    @text_message.incoming_message = false
    @text_message.sentstatus = false
    @text_message.phone = @client.phone

    if @text_message.scheduled_date == nil 
      @text_message.scheduled_date = Date.today
      # print time in Pacific time
      @text_message.scheduled_time = Time.now.in_time_zone("Pacific Time (US & Canada)")
      @text_message.send_text_message(@text_message.content, @text_message.phone)
    else
      @text_message.save 
    end
  end
  redirect_to clients_path
end

群组文本现在可以使用了:)