2014-08-28 41 views
0

我一直在尋找整個今天的解決方案,但無法找到答案。 這是一個簡單的預約系統。 我嘗試顯示錯誤消息,並在用戶提交無效日期/時間(例如:星期六,星期日/ 09:00-17:00之間的範圍)時呈現爲'新'。當條件落入驗證時,我不斷收到錯誤「表單中的第一個參數不能包含零或爲空」。 此外,當它不處於無效狀態時,不會顯示Flash消息,但會重定向到所需的頁面。Rails 4不工作閃光[:通知]和呈現

在_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 < ApplicationController中)

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 

閃光[:聲明]不會出現在任何條件。 渲染:新不起作用,並將錯誤作爲「形式中的第一個參數不能包含零或爲空」

在此先感謝。

+0

你必須改變這一行'任命= Appointment.new appointment_params'到'@appointment = Appointment.new appointment_params' – Pavan 2014-08-28 11:17:48

+0

謝謝,帕文。是。它在更改爲@appointment = Appointment.new appointment_params後生效。此外,我的朋友建議我在_form.html.erb中添加<%= flash [:notice]%>以顯示Flash通知。 – sugi1119 2014-08-28 11:25:57

回答

0

嘗試flash.now.notice = "Please chose the range between 09:00-17:00."

+0

謝謝Esha。根據Paven和我的朋友的說法,在將約會從約會改爲@appointment後,其工作正常。另外,我省略了添加<%= flash [:notice]%>以顯示Flash消息。 – sugi1119 2014-08-28 11:31:39