用户选择操作后如何获取当前日期和时间 [英] How to get the current date and time after a user select action
问题描述
我花了一些时间学习Rails,现在我正在做一个演示项目.
I spent time learning Rails and now I'm working on a demo project.
我希望用户选择是否需要对护理请求_form进行即时护理.如果用户选择否",则将要求用户从接下来的两个隐藏代码行中选择时间和日期,并保存start_time
和start_date
.如果用户选择是",则我想在用户单击表单上的提交"按钮后输入start_time
和start_date
字段的当前时间和日期.
I want the user to select whether or not they need immediate care on a care request _form. If the user selects "No" then the user will be asked to select a time and date from the next two hidden lines of code, saving start_time
and start_date
. If the user selects "Yes", I want to pass in the current time and date for the start_time
and start_date
fields after the user clicks the submit button on my form.
我知道我将使用if
/else
语句,隐藏函数和Time.now
/DateTime.now
方法,但是我对如何实现它们感到困惑.
I know I will use an if
/else
statement, a hidden function, and the Time.now
/DateTime.now
methods but I'm confused how to implement them.
<div class="row">
<div class="form-group">
<label class="control-label">Is immediate care needed?</label>
<select class="form-control edit">
<option>Yes</option>
<option>No</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group">
<label>Choose your start date.</label>
<%= f.text_field :start_day, placeholder: "MM/DD/YY", class: "form-control" %>
</div>
</div>
<div class="row">
<div class="form-group">
<label>Choose your start time.</label>
<%= f.text_field :start_time, placeholder: "08:00 AM", class: " form-control" %>
</div>
</div>
我查看了Stack Overflow,但没有发现类似的东西.
I looked at Stack Overflow but found nothing similar to this.
推荐答案
首先,您需要命名立即护理"选择字段,并提供选项值:
First, you need to name your "immediate care" select field, and supply values for options:
<select class="form-control edit" name="immediate_care">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
然后,您需要检查选定的值并设置当前日期&时间值(如果值为"yes")(可能是控制器内部的before_filter):
Then, you need to check the selected value and set current date & time values if the value is "yes" (maybe a before_filter inside a controller):
if params[:immediate_care] == 'yes'
current_time = Time.now
params[:start_time] = current_time.strftime '%l:%m %p'
params[:start_day] = current_time.strftime '%d/%m/%y'
end
请记住,这是一个简化的示例,因为您没有提供有关代码的更多详细信息.
Please keep in mind, that's a simplified example, since you did not provide more details about your code.
这篇关于用户选择操作后如何获取当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!