【发布时间】:2014-08-28 10:39:24
【问题描述】:
我今天整天都在寻找解决方案,但无法找到。 这是一个简单的约会系统。 当用户提交无效的日期/时间(例如:周六、周日/不在 09:00-17:00 之间)时,我尝试显示错误消息并呈现为“新”。当条件进入验证时,我不断收到错误“表单中的第一个参数不能包含 nil 或为空”。 此外,当它不处于无效状态时,不会出现 Flash 消息,而是重定向到所需的页面。
在_form.html.erb中
<%= form_for @appointment do |f| %>
<%= f.date_field :appointment_date, :min => Date.today %>
<%= time_field_tag 'appointment[appointment_time]' %>
<%= f.submit "Submit"%>
<% end %>
在 AppointmentsController 中(类 AppointmentsController
def create
appointment = Appointment.new appointment_params
date = params[:appointment][:appointment_date] + ' ' + params[:appointment][:appointment_time]
appointment.appointment_date = date
check = DateTime.parse(appointment.appointment_date.to_s)
if check.wday == 6 || check.wday == 7
flash[:notice] = "Please chose Mon-Fri."
render :new
elsif check.hour > 9 || check.hour < 17
flash[:notice] = "Please chose the range between 09:00-17:00."
render :new
elsif
appointment.save
flash[:notice]= "Appointment is saved."
redirect_to appointment
else
render :new
end
end
flash[:notice] 在任何情况下都不会出现。 render :new 不起作用并给出错误为“表单中的第一个参数不能包含 nil 或为空”
提前致谢。
【问题讨论】:
-
你必须把这行
appointment = Appointment.new appointment_params改成@appointment = Appointment.new appointment_params -
谢谢,帕万。是的。更改为@appointment = Appointment.new 约会参数后它可以工作。另外,我的朋友建议我在 _form.html.erb 中添加 以显示 flash 通知。
标签: ruby-on-rails-4