如何使用select_date传递参数日,月和年?

时间:2020-12-15 19:43:15

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

我有以下date_select,它显示三个下拉菜单,我可以从中选择月,日和年。

<%=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.

我不知道如何将参数传递到下一页。如何通过submit_tag或link_to传递月,日和年?我在一个名为generateDataForReport的视图页面中,我想在名为showGeneratedReport的同一控制器下捕获另一个视图中的参数。

1 个解决方案

#1


1  

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:

现在在下一页上,您的URL应该看起来像这样显示您的参数:

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] %>

#1


1  

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:

现在在下一页上,您的URL应该看起来像这样显示您的参数:

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] %>