user2004082 user2004082 - 3 months ago 10
Ruby Question

Rails 4 remote true form request (for an ajax loader) with browser url updated

I have a rails form that fires ajax by remote true as follows.

= form_tag(root_path, method: "get", remote: true) do ...


Works fine and all but how can I achieve the browser url to be updated with the search params ?
What I want to achieve is that after successful response, the browser should be updated with the correct url as in normal rails html request.

Eg. localhost:3000/my_controller?status=canceled&search=mysearchkey&commit=Search

Answer

In success response write this little js code

window.history.pushState("", "", '/my_controller?&status=canceledsearch=mysearchkey&commit=Search');

Updating address bar with new URL without hash or reloading the page

I hope it will help you out

Comments