Unable to make date_select form hidden in rails

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

I pass these html parameters to the form, but it seems like this never affects HTML:

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

Am I missing something? I also tried passing it to the HTML hash :html => { :type = "hidden" } , but that won't work either. Even when I try something else, like :class => "something" , it does not change the HTML. Is there anything special about the date_select ?

+4
source share
3 answers

date_select takes the discard_day , discard_month and discard_year to accomplish exactly what you are trying to achieve.

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

Behind the scenes, the following HTML is generated:

 <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 to your controllers.

+4
source

By name, date_select generates <select> elements. Without version (X) HTML, the select element supports the type attribute. If you want a hidden form field, you must use the hidden_field helper, which generates <input type="hidden"> elements.


(To answer your proposed usage question, for example :class => 'something' , the problem is that the options and html_arguments must be two separate hashes, but if you do something like this:

 <%= f.date_select :date, :default => @date, :class => 'something' %> 

.. the Ruby interpreter assumes that you have provided one hash, i.e. { :default => @date, :class => 'something' } (and really, can you blame him?), and since class not a valid key for the options hash are ignored. Instead, you should make it obvious to Ruby that these are two separate parameters by doing something like this:

 <%= f.date_select :date, :default => @date, { :class => 'something' } %> <%# Hey Ruby, this is a different Hash! ----^ %> 

See the difference? Of course, you could go to battle and be really obvious, for example:

 <%= f.date_select(:date, { :default => @date }, { :class => 'something' }) %> 

.. but it's ugly and egregious, so don't worry.)

+4
source

You can put it in a hidden div:

 <div style="display: none;"> <%= f.date_select :date, :default => @date, :type => "hidden" %> </div> 

This will allow you to have all the fields and hidden, which you can also use to select the date and time:

 <div style="display: none;"> <%= f.datetime_select :date, :default => @date, :type => "hidden" %> </div> 
+2
source

Source: https://habr.com/ru/post/1379247/


All Articles