2016-05-01 15 views
1
<tr> 
    <td>Bill:</td> 
    <td colspan="2"><%= select_tag 'bill_hours', options_for_select(@hours, 0) %>:<%= select_tag 'bill_minutes', options_for_select(@minutes, 0) %></td> 
    </tr> 
    <tr> 
    <td>Nonbill:</td> 
    <td colspan="2"><%= select_tag 'nonbill_hours', options_for_select(@hours, 0) %>:<%= select_tag 'nonbill_minutes', options_for_select(@minutes, 0) %></td> 
    </tr> 
    <tr> 
    <td>Category:</td> 
    <td colspan="2"> 
    <%=select_tag 'cat_type_id',options_for_select(@cat_type_list,@selected_cat_type), {:prompt=>"Select Category Type"}%><br/> 
     <select id="activity_category_id" prompt="Select Category" name="activity[category_id]" style="width:300px;"> 
     <option value=''>Select Category</option> 
     <%@cat_list.each do |c|%> 
     <option value="<%=c.id%>" <%=([email protected]? && @activity.category_id==c.id) ? "selected" : "" %> class="<%=c.cat_type_id.nil? ? '':c.cat_type_id%>"><%=c.short_description.nil? ? c.description : c.short_description%></option> 
     <%end%> 
     </select></td> 
    </tr> 
    <tr> 
    <td>Location:</td> 
    <td colspan="2"><%=select 'activity', 'location_id', @locations, {:prompt=>"Select Location"}, :required => true %><br/><%=select 'activity', 'location_modifier_id', @location_mods, {:prompt=>"Select Modifier"}, :required => true %></td> 
    </tr> 

Ich habe den obigen Code als Beispiel. Wie kann ich den "activity, location_id" genau dann herstellen, wenn "bill_hours" oder "bill_minutes" oder "nonbill_hours" oder "nonbill_minutes"> 0 ist?validieren, wenn ein anderes Feld größer als

Antwort

1

Übergeben Sie den Schlüssel :if, um eine Bedingung für Ihre Validierung zu verwenden.

In Ihrem Modell:

validates_presence_of :location_id, if: -> do 
    %i(bill_hours bill_minutes nonbill_minutes nonbill_hours).any? { |attr| read_attribute(attr) > 0 } 
end 
+0

ich ** nicht definierte Methode '>‘für nil: NilClass ** wenn ich Ihren Vorschlag versuchen. –

+1

Sind 'bill_hours'' bill_minutes' 'nonbill_minutes'' nonbill_hours' alle Spalten in Ihrem Modell? –

+0

Sie hatten Recht, musste geändert werden (bill_duration nonbill_duration) Dank Anthony –

Verwandte Themen