Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant make form date_select hidden in rails

I am trying to create a form which loads upon a user clicking a date in a calendar, the form then is passed the date that is clicked through the URL and the controller assigns that date to the @date variable. I then create a date_select element and assign it the @date variable. This works fine but since I do not want the user to be able to edit the date in the form I want it to be hidden.

I pass these html options to the form but it doesn't seem to ever effect the HTML:

<%= f.date_select :date, :default => @date, :type => "hidden" %>

Am I missing something? I also tried passing it in an HTML hash :html => { :type = "hidden" } but that doesn't work either. Even when I try something different like :class => "something" it doesn't change the HTML. Is there something special about the date_select helper?

like image 658
Graeme Avatar asked Dec 22 '22 07:12

Graeme


1 Answers

date_select accepts the options discard_day, discard_month and discard_year to do exactly what you are trying to achieve.

<%= f.date_select :date, { :discard_day => true, :discard_month => true, :discard_year => true } %>

Behind the scenes, it generates the following HTML code:

<input id="record_date_3i" name="record[date(3i)]" type="hidden" value="5" />
<input id="record_date_2i" name="record[date(2i)]" type="hidden" value="1" />
<input id="record_date_1i" name="record[date(1i)]" type="hidden" value="2012" />

No CSS tricks, no changes in your controllers.

like image 179
ismriv Avatar answered Dec 24 '22 02:12

ismriv