2016-05-01 2 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> 

У меня есть приведенный выше код в качестве образца. Как я могу сделать "activity, location_id" необходимым тогда и только тогда, когда "bill_hours" или "bill_minutes" или "nonbill_hours" или "nonbill_minutes" is> 0?подтвердите, если другое поле больше

ответ

1

Пройдите ключ :if, чтобы использовать условие при вашей проверке.

В модели:

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

я получаю ** неопределенный метод '>» для ноль: NilClass **, когда я попробовать свои предложения. –

+1

В столбце "bill_hours" 'bill_minutes'' nonbill_minutes' 'nonbill_hours' все столбцы в вашей модели? –

+0

Вы были правы, необходимо было изменить на (bill_duration nonbill_duration) Спасибо Anthony –

Смежные вопросы