Hommer Smith
Hommer Smith

Reputation: 27852

Rails - Reload action with new value in params

I have the following relation in my model:

A user can have several consumers

According to that, I call several actions from different controllers that depend on that consumer_id in the URL. So, I do stuff like:

/:consumer_id/products/all
/:consumer_id/locals/all?params...

I want to be able to given that I am in a particular view, let's say /3/products/all, be able to refresh, redirect or whatever is the best to /4/products/all, with a form that shows the different consumers attached to that user.

I know how to display the form, but I fail to see which action should I put given that I want to load the current controller, current action and the current params, except that I want to change the params[:consumer_id] and the session[:consumer_id] to the one chosen by the user in the form of the view.

What's the best or appropriate way of doing so?

Upvotes: 3

Views: 3541

Answers (3)

Sprachprofi
Sprachprofi

Reputation: 1259

This is a link to the same page with the same parameters and one additional parameter:

<%= link_to "Show more results", url_for(params.merge(:per => 100)) %>

If you want to modify a parameter rather than add a new one, you can modify the params hash just as you would modify any hash, and use url_for again.

Upvotes: 3

allenhwkim
allenhwkim

Reputation: 27738

Your question is not clear. You want to choose consumer_id from your form, then use that consumer id inside the same page or for links to other pages?

If you want to use the consumer id for the current page, you need to use javascript or JQuery to update all attributes of the current page.

If you want to use that consumer id for links,

  • you may also use JQuery to update all links on your page
  • Or, you can submit the form to the server with new consumer_id.

Upvotes: 0

Carlos Ramirez III
Carlos Ramirez III

Reputation: 7434

Inside the view you can access both the current controller using controller_name and the current action using action_name

Upvotes: 2

Related Questions