所以这里是场景。我正在使用 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 的初学者,如果这个问题的格式不完美,请提前抱歉。还在学习。当我使用“-”、“+”和“”时,我的方法不断出错。所以我把这些东西放在视图中。这就是为什么它看起来很奇怪。你们有什么感想?