1

so there I have this code in rails

<%= f.label :year, "Year:" %><br />
<%= f.text_field :year %></p>

that returns in html a code like this one

< input id="car_year" name="car[year]" size="30" type="text" class="hasDatepicker"><br />

see the class="hasDatepicker"? I use the jquery to get the calendar to select dates this one is working good

but here is another one (using meta_search https://github.com/ernie/meta_search)

<td>
<%= f.label :year_gte, "Years from" %><br />
<%= f.text_field :year_gte, :size  => 8 %></td>

that outputs an html code like this one

< input id="search_year_gte" name="search[year_gte]" size="8" type="text"><br />

obvius this one is missing the class="hasDatepicker" that needs the jquery to display the calendar

The question is: How do I get the class thing to display in that field Thanks all for your time.

Update 2 I just found the problem I need an javascript code in my view for search_year_gte as I have one for car_year looks like

<script>
    $(function() {
        $( "#search_year_gte" ).datepicker({
      dateFormat: 'yy-mm-dd',
      yearRange: '1980:2012',
            changeMonth: true,
            changeYear: true
        });
    });
</script>

<script>
    $(function() {
        $( "#search_year_lte" ).datepicker({
      dateFormat: 'yy-mm-dd',<br />
      yearRange: '1980:2012',<br />
            changeMonth: true,
            changeYear: true
        });
    });
</script>

but can I just use one script for both values? for search_year_lte and search_year_gte

rmagnum2002
  • 11,341
  • 8
  • 49
  • 86

1 Answers1

1

Did you try adding the class as an attribute?

<%= f.text_field :year_gte, :size => 8, :class => "hasDatepicker" %>

Have a look at the documentation if you want to know what attributes you can pass to the text_field helper.

Cimm
  • 4,653
  • 8
  • 40
  • 66
  • yes.. and I just found the problem I need an javascript code in my view for search_year_gte as I have one for car_year – rmagnum2002 Oct 08 '11 at 18:23
  • can I add 2 values in one script? I mean thei are both for the action, so I don't use twice almost the same code. Thank any way for quick reply. – rmagnum2002 Oct 08 '11 at 18:29
  • I think this is more a JS question than a Rails one but sure, you are doing the same thing twice so it makes sense to only have one JS function. Why do you use `#search_year_lte`? That's an id and should be unique. I would add your `hasDatePicker` class to all text fields that need a data picker and call `$('.hasDatePicker')` in your JavaScript. Does this make sense? – Cimm Oct 08 '11 at 18:41
  • I am not into java at all.. I am new in rails too so I managed to pull it off this way, I dont't like to have to much code for the same action but it's the way I can do it. I guess it will take time till I'll be able to do your way.. offcourse makes more sense. Thanks – rmagnum2002 Oct 08 '11 at 18:58
  • First of all [Java != JavaScript](http://www.htmlgoodies.com/beyond/javascript/article.php/3470971/Java-vs-JavaScript.htm). The names are confusing but it is NOT the same language at all. Secondly, I think you may want to have a look at [JQuery](http://jquery.com) and how to select DOM elements. `$('#me')` will select the element with *id* 'me'. `$('.me')` will select all elements with the *class* 'me'. – Cimm Oct 08 '11 at 19:09
  • offcourse java is not javascript, I jut used a shortcut :) thanks for advices about jquery, I'll take a look. ;) – rmagnum2002 Oct 08 '11 at 19:16