rails ajax redirect - ruby-on-rails

Here is my use case
I have a search model with two actions search_set and search_show.
1 - A user loads the home page which contains a search_form, rendered
via a partial (search_form).
2 - User does a search, and the request goes to search_set, the search
is saved and a redirect happens to search_show page which again
renders the search_form with the saved search preferences. This search
form is different than the one if step1, because it's a remote form
being submitted to the same action (search set)
3 - Now the user does another search, and the search form is submitted
via ajax to the search_set action. The search is saved and executed
and now I need to present the result via rjs templates (corresponding
to search_show). I am told that if the request is xhr then I can't
redirect to the search_show action? Is that right? If yes, how do I
handle this?
Here is my controller class
http://pastie.org/993460
Thanks

That's right. Either make the request non-XHR and redirect as normal, or you could try rendering the URL you want to redirect to as text or part of a JSON object which your AJAX request then uses to call document.location.href = [whatever] (but this seems hacky).
Right now what's happening is your XHR request is returning the result of the redirect, and not actually redirecting the page that made the XHR request.

Related

Cannot submit form drawn by Rails Turbo Frame response

A form inside a turbo_frame is blocked from being submitted a second time.
Both the current page and the form action are the same path.
I see forms now have to redirect to a new location or return an error code: turbo/pull/39.
I have a form on every index view sending a GET request to the index action, which filters the records based on the url query parameters. This works great. Is this no longer possible with latest Rails? Is there a workaround that could be applied here, to avoid creating a new route for the form action?
These are idempotent filter/search requests, hence the GET method. A form is a handy way to collect and send the filter values. Is it correct/intentional that Rails with Hotwire insists on a redirect in this scenario?
Update:
It works if I submit the form as xhr instead of using Turbo. I add local: false and data-turbo: false to the form. Then add index.js.erb with document.getElementByID(<id of turbo frame>) and set its outerHTML to the rendered partial. Not sure if this will stop working due to deprecated ujs features (maybe mrujs can step in then).

difference between Html.BeginForm() and ajax.beginform()

what is the difference between Html.BeginForm() and Ajax.Beginform() in MVC3. I just want to know the scenarios where I can use Html.BeginForm() and where I can use Ajax.Beginform().
Ajax
Won't redirect the form even you do a RedirectAction().
Will perform save , update and any modification operations asynchronously.
Validate the Form using FormMethods - OnSubmit. So you are abort the Post
This creates a form that submits its values using an asynchronous ajax request. This allows a portion of the page to be update without requiring that the entire page be refreshed.
Html
Will redirect the form.
Will perform operations both Synchronously and Asynchronously (With some extra code and care).
Html.BeginForm will always use RouteTable to detrmine the action attribute value.
This will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process.
Html.BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process.
Ajax.BeginForm() creates a form that submits its values using an asynchronous ajax request. This allows a portion of the page to be update without requiring that the entire page be refreshed.
Html.BeginForm() will use simple posting on page, it means your page will be refreshed when you post your form.
when Ajax.BeginForm() will use ajax posting on page, it means your page will not be refreshed when you post your form.
#Html.BeginForm is used to post the data by full page refresh whereas #Ajax.BeginForm performs Post back function and allows some portion of Html to be reloaded rather than overall page refresh.

Accessing Page URL from within Partial View in MVC

I have a page which has a partial view included within a div called test. I may request more content to be placed within this div by receiving back a PartialViewResult.
I want to be able to obtain the url of the page (i.e. what is shown in the address bar) from within the PartialView code but when I use Request.Url, it gives me the URL of the PartialView only.
Is what I'm trying to do possible at all?
OK, figured out another way of doing it - now using the HTTP_REFERER Request.ServerVariable to work out what page requested it. Not a disaster if the HTTP_REFERER isn't populated, have got a fallback for that

ASP.NET MVC - How can I pass FormCollection data in a post to another Action?

I have a page "Results" with a form and when "submit" is clicked, the form is submmited to another Action. So far, so good...
But, this works fine just if user is logged in. If not, he will be redirected to "Login" page and my FormCollection loses its data.
Is there a way to persist this data without using TempData??
Thanks!!
I don't think that's possible. The only thing the system remembers during the redirect to the login page is the 'return url'. No post data is saved (this could be megabytes of data...)
You can use the Session object as alternative, or make sure that the user is logged in before you post.
Or, if it's just a search result, try to live without the POST, and use a GET (which also has other advantages)
I would prefer to disallow unauthorized user to visit "Results" page or at least to show him message "Please login first" instead of the form.

request.referer with anchor

is there a way to get the referer with the anchor from the referer page..
the situation is - i have a search page which loads results (users) below the search box using ajax. i am changing the hash/anchor on the fly when the search keyword is typed in.
so it becomes http://localhost/users/search#foo
the user gets link in the ajax result to edit the user. i want the user to come back to this same search result after he is done editing the user.
Anchors are not included in the referrer. You'll need to use query parameters or define the return URL (with the anchor) in the session before updating the user.

Resources