我有一個問題的方法,並不知道什麼是最合適的方法將使這項工作。這裏的背景開始:Rails的3種方式與多個if語句和表單創建/新
有兩種模型我正在使用程序和約會。約會模型屬於程序模型和過程模型has_many約會。
現在在過程模型中有兩個關鍵點需要關注,而兩個關鍵點。
attr_accessible :visits, :occurence
參觀是次安排的約會(一個或多個)的具體數目。 發生次數是訪問次數。一個例子是visits: "5", occurence: "weekly"
所以,當我提出我的形式,我想編寫一個看起來在兩個visits: "x"
和occurence: ["weekly", "biweekly", "monthly"]
來然後創建一個if或開關的方法 - PHP並切換仍在調查紅寶石版本 - 但我懷疑有一個優雅的方式來寫這個。
我現在的建立方法是這樣的:
def create
@appointment = Appointment.new(params[:appointment])
set_variables
if @appointment.save
flash[:success] = "Appointment scheduled!"
redirect_to patient_path(@current_patient)
else
redirect_to patient_path(@current_patient)
flash[:error] = "Appointment Date and Time cannot be blank, please try again."
end
end
什麼是解決一個識別occurence: ["weekly", "biweekly", "monthly"]
,然後基於類似的東西處理visits: "x"
的最佳方式):
if @appointment.occurence == "weekly"
(x-1).times do |n|
submit same params but change within params appointment_date: = ((@appointment.appointment_date) + (n+1).week.to_formatted_s(:db)
@appointment.save
end
end
.. 。以此類推,使用(n+1).month
每月發生(n+2).day
和每兩週發生一次。
預先感謝您,希望澄清一些事情。只需要注意一件事,我是否需要在數據庫visits:
和occurence:
中存儲,我懷疑不是,但想確定它們是在使用models_controller創建函數時使用的。
這是一個可愛且值得歡迎的改變!感謝您花時間寫這篇文章。 – sandovalg 2013-06-03 06:52:52