1

これがシナリオです。Rails 4 と Rufus Scheduler を使用して、将来の特定の時間に SMS を送信するアプリを作成しています。これまでのところ、ボタンをクリックするだけで SMS を送信したい場合、SMS が機能するようになりました。また、以下のような独自のコードで設定したタイマーでそれらを送信することもできます。これにより、メッセージを将来送信することができます。基本的に、タイマーを設定し、タイマーが鳴ったらメソッドを呼び出して SMS メッセージを送信します。

これはすべて素晴らしいことですが、最後に行う必要があるのは、ユーザー入力を受け取り、それを日付/時刻/タイムゾーンがある場所に配置するメソッドを作成することです。私の new.html.erb では、すでにこの情報をすべて取り込んでいますが、そこから取得してスケジューラ アクションに入れる方法がわかりません。

これは機能します:

scheduler = Rufus::Scheduler.new
      scheduler.at '2014-02-16 5:47 PM Pacific Time' do
      @sms_message.send_text_message
      end

これは私がすでに試したことですが、運がありません。

モデルはこちら。

class SmsMessage

include Mongoid::Document
include Mongoid::Timestamps

field :from, type: String
field :to, type: String
field :body, type: String

field :year, type: String
field :month, type: String
field :day, type: String
field :timehour, type: String
field :timeminute, type: String
field :timezone, type: String
field :ampm, type: String

belongs_to :user

def set_time

puts year + month + day + " " + timehour + timeminute + " " + ampm + " " + timezone 

end


def send_text_message

ts = self.user.twilio_sid
tt = self.user.twilio_token

sent_to_number = to
sent_from_number = self.user.twilio_number
  message_to_send = body

@twilio_client ||= Twilio::REST::Client.new ts, tt

@twilio_client.account.sms.messages.create(

  :from => "#{sent_from_number}",
  :to => "#{sent_to_number}",
  :body => "#{message_to_send}"
)

end

end

で、コントローラーはこちら。

def create   
if current_user

  @sms_message = current_user.sms_messages.build(params[:sms_message].permit(:to, :from, :body))

    if @sms_message.save

      scheduler = Rufus::Scheduler.new
      scheduler.at @sms_message.set_time do
      @sms_message.send_text_message
      end

      flash[:notice] = "Your text has been scheduled!"
      redirect_to sms_messages_path

    else
      render 'new'
    end

else new_user
  redirect_to new_user_path
end

終わり

そしてビュー:

<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
    <%= form_for(@sms_message) do |f| %>

        To: <%= f.text_field :to, class: "form-control" %>
        Body: <%= f.text_area :body, class: "form-control" %><br />
        <%= f.select :month, ['01-', '02-', '03-','04-',
            '05-', '06-', '07-', '08-', '09-', '10-', '11-', '12-'] %>
        <%= f.select :day, ['1', '2', '3', '4', '5', '6', '7', '8', '9',
        '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23',
        '24', '25', '26', '27', '28', '29', '30', '31'] %>
        <%= f.select :year, ['2014-', '2015-', '2016-', '2017-'] %>
        at <%= f.select :timehour, ['1:', '2:', '3:', '4:', '5:', '6:', '7:', '8:', '9:',
            '10:', '11:', '12:']%>
        <%= f.select :timeminute, ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
            '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24',
            '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38',
            '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52',
            '53', '54', '55', '56', '57', '58', '59', '60'] %>
        <%= f.select :ampm, ['AM', 'PM'] %>
        <%= f.select :timezone, ['Pacific Time', 'Eastern Time', 'Central Time',
            'Mountain Time', 'Alaska Time', 'Hawaii-Aleutian Time', 'Samoa Time'] %><br /><br /><br />
        <%= f.submit "Send Text Message", class: "btn btn-success"  %>
    <% end %>
</div>
<div class="col-md-3"></div>

私はこれを 3 時間以上クラックしようとしてきましたが、運がありませんでした。私は Rails の初心者です。この質問が完全にフォーマットされていない場合は、事前に申し訳ありません。まだまだ勉強中。「-」、「+」、および「 」を使用すると、メソッドでエラーが発生し続けました。だから私はそれらのものをビューの中に入れました。それが奇妙に見える理由です。皆さんはどう思いますか?

4

2 に答える 2

0

app/model/sms_message.rb

set_time関数を次のように変更します。

def set_time
  year + month + day + " " + timehour + timeminute + " " + ampm + " " + timezone 
end

これは文字列をing するreturn代わりにします。print

app/controllers/sms_message_controller.rbに次を追加します。

def create   
  if current_user

    @sms_message = current_user.sms_messages.build(sms_message_params)
    if @sms_message.save

    scheduler = Rufus::Scheduler.new
    scheduler.at @sms_message.set_time do
      @sms_message.send_text_message
    end

    flash[:notice] = "Your text has been scheduled!"
    redirect_to sms_messages_path

  else
    render 'new'
  end

else new_user
  redirect_to new_user_path
end

private

def sms_message_params
  params.require(:sms_message).permit(:from, :to, :body, :month, :day, :year, :timehour, :timeminute, :ampm, :timezone)
end
于 2014-02-17T04:28:47.887 に答える