当前位置: 动力学知识库 > 问答 > 编程问答 >

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?

网友答案:

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.

网友答案:

Per the name, date_select generates <select> elements. In no version of (X)HTML does the select element support the type attribute. If you want a hidden form field then you should use the hidden_field helper, which generates <input type="hidden"> elements.


(To answer your implied question about using e.g. :class => 'something', the problem is that the options and html_arguments parameters 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 supplied a single hash, i.e. { :default => @date, :class => 'something' } (and really, can you blame it?), and since class isn't a valid key for the options hash it's ignored. Instead you have to make it obvious to Ruby that these are two separate parameters by doing something like this instead:

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

See the difference? Of course you could go bonkers and be really obvious, e.g.:

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

..but that's ugly and egregious so don't bother.)

网友答案:

You can put it inside a hidden div:

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

That will allow you to have all the fields and hidden you can also use for date and time select:

<div style="display: none;">
  <%= f.datetime_select :date, :default => @date, :type => "hidden" %>
</div>
分享给朋友:
您可能感兴趣的文章:
随机阅读: