How do I pass parameters day, month and year using select_date?

1.1k views Asked by At

I have the following date_select, which displays three drop-down menus, from which I can select the month, day, and year.

<%=date_select("dateName", "dateMethod", :order => [:month, :day, :year])%>

I don't know how to pass the parameters to the next page. How can I pass the month, day and year with a submit_tag or a link_to? I am in a view page named generateDataForReport, and I want to catch the parameters in another view under the same controller named showGeneratedReport.

1

There are 1 answers

0
fatfrog On

If you look at your console when you submit the form, you can see how these parameters are named:

"dateName"=>{"dateMethod(2i)"=>"12", "dateMethod(3i)"=>"1", "dateMethod(1i)"=>"2013"

In your controller, you can grab the parameters like this:

Year:

params[:dateName]['dateMethod(1i)']

Month:

params[:dateName]['dateMethod(2i)']

Day:

params[:dateName]['dateMethod(3i)']

You can assign them to variables in your controller to make them easier to deal with:

day = params[:dateName]['dateMethod(3i)']
month = params[:dateName]['dateMethod(2i)']
year = params[:dateName]['dateMethod(1i)']

If you want to pass these params to your next page, then do this in your redirect:

redirect_to report_path(@report, :d => day, :m => month, :y => year) 

Now on the next page, your URL should look something like this showing your params:

http://localhost:3000/reports/28?d=1&m=12&y=2013

Now to display them on the page you do this:

  <%= params[:d] %>
  <%= params[:m] %>
  <%= params[:y] %>