Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Are we asking an interface question here (i.e. you want a widget) or an ActiveRecord question?</p> <h1>Date Picking Widgets</h1> <p>1) <strong>Default Rails Solution</strong>: See <code>date_select</code> documentation <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html" rel="noreferrer">here</a>.</p> <p>2) <strong>Use a plugin</strong> : Why write code? I personally like the <a href="http://code.google.com/p/calendardateselect/" rel="noreferrer">CalendarDateSelect</a> plugin, using a pair of the suckers when I need a range.</p> <p>3) <strong>Adapt a Javascript widget to Rails</strong>: It is almost trivial to integrate something like the Yahoo UI library (YUI) <a href="http://developer.yahoo.com/yui/calendar/" rel="noreferrer">Calendar</a>, which is all Javascript, to Rails. From the perspective of Rails its just another way to populate the <code>params[:start_date]</code> and <code>params[:end_date]</code>. YUI Calendar has native support for ranges.</p> <h1>Getting the data from the Widgets</h1> <p>1) <strong>Default Rails Solution</strong> See <code>date_select</code> documentation <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html" rel="noreferrer">here</a>.</p> <pre><code>#an application helper method you'll find helpful #credit to http://blog.zerosum.org/2007/5/9/deconstructing-date_select # Reconstruct a date object from date_select helper form params def build_date_from_params(field_name, params) Date.new(params["#{field_name.to_s}(1i)"].to_i, params["#{field_name.to_s}(2i)"].to_i, params["#{field_name.to_s}(3i)"].to_i) end #goes into view &lt;%= date_select "report", "start_date", ... %&gt; &lt;%= date_select "report", "end_date", ... %&gt; #goes into controller -- add your own error handling/defaults, please! report_start_date = build_date_from_params("start_date", params[:report]) report_end_date = build_date_from_params("end_date", params[:report]) </code></pre> <p>2) <strong>CalendarDateSelect</strong>: Rather similar to the above, just with sexier visible UI.</p> <p>3) <strong>Adapt a Javascript widget</strong>: Typically this means that some form element will have the date input as a string. Great news for you, since Date.parse is some serious magic. The params[:some_form_element_name] will be initialized by Rails for you.</p> <pre><code>#goes in controller. Please handle errors yourself -- Javascript != trusted input. report_start_date = Date.parse(params[:report_start_date]) </code></pre> <h1>Writing the call to ActiveRecord</h1> <p>Easy as pie.</p> <pre><code> #initialize start_date and end_date up here, by pulling from params probably @models = SomeModel.find(:all, :conditions =&gt; ['date &gt;= ? and date &lt;= ?', start_date, end_date]) #do something with models </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload