简体   繁体   English

在动作之间保存参数

[英]Save parameters between actions Rails

I have a controller, named OrdersController. 我有一个名为OrdersController的控制器。 And db table "Orders" with information about shop orders. 和数据库表“订单”与有关车间订单的信息。 In a index page (localhost/) I must show two buttons: today orders and orders in range (with two inputs). 在索引页面(localhost /)中,我必须显示两个按钮:今天的订单和范围内的订单(带有两个输入)。

First button should open a page with list of orders, created today (Date.current for example). 第一个按钮应打开一个包含今天创建的订单列表的页面(例如,Date.current)。 Second button should open a page with list of orders, that was created in date range, specified in the input . 第二个按钮应打开一个包含订单列表的页面,该页面是在输入中指定的日期范围内创建的。

When I click on some order the details page is opens for it. 当我单击某些订单时,将为其打开详细信息页面。 There I can edit some fields of order and click "Update" button (it must update @order and redirect to (!)previous page - if it is today orders, then open a todays order. If it is a orders in a date range - then show orders created in range that was specified). 在这里,我可以编辑一些订单字段,然后单击“更新”按钮(它必须更新@order并重定向到(!)上一页-如果是今天的订单,则打开今天的订单。如果它是日期范围内的订单-然后显示在指定范围内创建的订单)。

The question: what is the best practise to pass parameters between actions? 问题:在动作之间传递参数的最佳实践是什么? How can I say to my "update" action where to redirect_to (to page with today orders or to page with orders in range) after updating? 在更新后,我如何对我的“更新”操作说在哪里redirect_to(到今天订单的页面或到范围内订单的页面)? It must be a hiiden tag with params[:start_date] or something else? 它必须是带有params [:start_date]的hiiden标记或其他名称?

The same is for the "back" button on the Order details page. “订单详细信息”页面上的“后退”按钮也是如此。 It must return to the previous page (saving all the parameters that was passed for). 它必须返回到上一页(保存为之传递的所有参数)。 I was thinking about 我在想

link_to 'Back', :back

But I heared it's not a good solution. 但是我听说这不是一个好的解决方案。

Thx! 谢谢!

I have used it in redirect_to not in link_to . 我已经在redirect_to而不是link_to使用了它。

:back is a good option in update or create actions, since redirect is done through a GET, so when you POST to orders or PUT to orders/1 I suppose you will redirect user to orders/new or orders/1/edit . :backupdatecreate操作中的一个不错的选择,因为重定向是通过GET完成的,所以当您发布到orders或将PUT放置到orders/1我想您会将用户重定向到orders/neworders/1/edit

But as you want to redirect to /orders maintaining your search page, your hidden field makes more sense, in app/views/orders/_form.html.erb inside the form block: 但是,由于要重定向到/orders维护搜索页面, app/views/orders/_form.html.erb在表单块内的app/views/orders/_form.html.erb ,隐藏字段更有意义:

<%= hidden_field_tag :redirect_url, request.referrer %>

Then in your orders_controller in update or create actions: 然后在您的orders_controller中进行更新或创建操作:

redirect_to params[:redirect_url]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM