Nobita
Nobita

Reputation: 23713

Rails: Modify URL in view and refresh with that new value

I am dealing with URLs of this style:

http://mysite/tables/134/X/Y
http://mysite/tables/134/X/Z

The number 134 is recognized as :table_id in the related controllers. I would like to be able to (from the view), change the :table_id and call the same URL. So, something like:

http://mysite/tables/135/X/Y

I have tried doing it like this:

<%= collection_select("params", :table_id, @tables , :id, :id, {:prompt => true}, :onchange => "location.href = ''") %>

But the params[:table_id] that I get in the controller it keeps being the old one that I had in the URL. So my question is:

Do I need to build the URL by myself? Can't I just change params[:table_id] and reload the same URL?

Upvotes: 2

Views: 1099

Answers (1)

chug2k
chug2k

Reputation: 5220

Short answer: Yes, you need to build the URL.

Reason: The variable params[:table_id] comes from the URL. I'm sure you've seen website urls of the form http://www.foo.com/?var=baz. If you do this in Rails, it will set params[:var] to be baz. That's basically what happens here with table_id.

A basic principle of REST is that the HTTP transactions are stateless. (Cookies and session storage are an exception, but that's not relevant here.)

Upvotes: 1

Related Questions