我有这段代码允许用户单击添加日期以显示另一行来指定日期时间。屏幕截图显示用户输入了两个日期:
<fieldset class="date_fieldset">
<%= f.label :user_event_date, "Date", class: "info_inline_control info_label" %>
<%= f.text_field :user_event_date, class: 'datepicker' %>
<div class="user_event_inline_container margin_left_ten padding_right_gone">
<%= f.label :start_time, "Start", class: 'info_inline_control info_label five_margin_right' %>
<%= f.time_select :start_time, {:ampm => true}, class: (field_class(@user_event, :start_time) + 'info_inline_control') %>
</div>
<div class="user_event_inline_container margin_left_ten padding_right_gone">
<%= f.label :end_time, "End", class: 'info_inline_control info_label five_margin_right' %>
<%= f.time_select :end_time, {:ampm => true}, class: (field_class(@user_event, :end_time) + 'info_inline_control') %>
</div>
<%= f.hidden_field :_destroy %>
<%= link_to "delete", '#', class: 'remove_fields margin_left_ten' %>
</fieldset>
<script type="text/javascript">
$(function() {
$('.datepicker').datepicker({
dateFormat: "dd-mm-yy"
});
});
</script>
但是,如果表单上其他一些不相关的字段出现验证错误,并且在表单顶部报告错误,则用户指定日期的两行将消失,单击“添加日期”按钮会使用户重新开始以再次指定日期。如何防止页面刷新以显示清除用户选择日期的验证错误?
以下是新的、编辑和创建控制器操作方法:
def new
if !signed_in?
redirect_to signin_path
else
@user_event = UserEvent.new
@user_event.event_dates.build
end
end
def edit
end
def create
@user_event = current_user.user_events.build(params[:user_event])
if @user_event.save
flash[:success] = "Event was successfully posted!"
redirect_to root_url
else
render action: :new
end
end
与往常一样,非常感谢您提前回答!