Andrew Smith
Andrew Smith

Reputation: 63

Save parameters between actions Rails

I have a controller, named 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).

First button should open a page with list of orders, created today (Date.current for example). 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).

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? It must be a hiiden tag with params[:start_date] or something else?

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!

Upvotes: 1

Views: 837

Answers (1)

sites
sites

Reputation: 21785

I have used it in redirect_to not in 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.

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:

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

Then in your orders_controller in update or create actions:

redirect_to params[:redirect_url]

Upvotes: 1

Related Questions