Reputation: 10823
I'm creating a Rails 3.1 app and I want add in my view a checkbox tag that I should use to enable/disable editing of many fields; I explain better: I've a model named Hardware and I've two Hardware fields named 'warranty_start' and 'warranty_end'. and this is code relative to these:
<div class="group">
<%= f.label :warranty_start, t("activerecord.attributes.warehouse.warrenty_start", :default => "Inizio garanzia"), :class => :label %>
<%= f.date_select :warranty_start, :order => [:day, :month, :year], :class => 'date_select', :start_year => Time.now.year-3, :end_year => Time.now.year + 10 %>
</div>
<span class="description">Data di inizio Garanzia</span>
<div class="group">
<%= f.label :warranty_end, t("activerecord.attributes.warehouse.warrenty_end", :default => "Fine garanzia"), :class => :label %>
<%= f.date_select :warranty_end, :order => [:day, :month, :year], :class => 'date_select',:start_year => Time.now.year-3, :end_year => Time.now.year + 10 %>
</div>
<span class="description">Data di fine Garanzia</span>
I want a checkbox that if enabled, let user to choose a date for warranty_start and warranty_end. In witch way can I do?
Upvotes: 2
Views: 1605
Reputation: 548
You must use JavaScript for that. Maybe use jQuery.
HTML:
<div id="warranty">
<!-- Here you put inputs with warranty start and end -->
</div>
JavaScript:
$('#id_of_checkbox').click(function(){
$('#warranty').css('display', ($(this).is(':checked') ? 'block' : 'none'));
});
In Rails you must define in model (change :column_checkbox to column name:
class SomeModel < ActiveRecord::Base
validates :warranty_start, :presence => true, :if => :warranty_want?
validates :warranty_end, :presence => true, :if => :warranty_want?
def warranty_want?
warranty_checkbox == true
end
# rest of your model
end
Upvotes: 2