1

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!

1
  • If the "parameters" are simple query string params in the URL, you could use the session to store the previous location and then redirect the user to that after update. There are a number of SO questions related to this. Here's one example: stackoverflow.com/questions/16630652/… Commented Dec 2, 2013 at 20:00

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, yes :) I didn't think in that way. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.