0
我使用:if
選項中包含以下mongoid驗證選項:Mongoid和
型號:
field :categories, type: Array
belongs_to :personality
validates_presence_of :categories, :if => Proc.new{ self.personality.name == "Parking" }
查看:
<%= f.collection_select :personality_id, @personalities, "id", "name" %>
...
<!-- TODO (JavaScript): Following checkboxes would only appear when personality "Parking" is selected -->
<input type="checkbox" name="structure[categories][]" value="Top floor" />
...
<input type="checkbox" name="structure[categories][]" value="Ground floor" />
...
<input type="checkbox" name="structure[categories][]" value="Basement" />
...
控制器:
if @structure.update_attributes(params[:structure])
flash[:success] = "Structure was successfully updated."
redirect_to admin_structure_path
else
render :action => "edit"
end
當我嘗試編輯現有記錄時,如果將個性更改爲Parking,並且categories
(複選框)中沒有值,它將忽略驗證。在診斷之後,在我看來它正在驗證保存的(或舊的)值personality_id
,而不是最新的值。
請指教。
請問您能提供更多代碼嗎? –
謝謝,但我需要了解'groupoid'來自哪裏:) –
請看看編輯過的問題。 – Annie