Forum: Ruby on Rails AJAX to submit form, but reload the whole page

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ngising (Guest)
on 2006-01-05 03:47
I'm using ROR 1.0 and the included prototype.js to do some AJAX stuff.

I'm using the form_remote_tag to submit a login form using AJAX.
So, any error/failed login will be displayed using AJAX. But, for
successful login, I would want it to reload the wholepage, not just
update the <DIV>.

How can I achieve this?
Rick O. (Guest)
on 2006-01-05 04:18
(Received via mailing list)
On 1/4/06, ngising <removed_email_address@domain.invalid> wrote:
> I'm using ROR 1.0 and the included prototype.js to do some AJAX stuff.
>
> I'm using the form_remote_tag to submit a login form using AJAX.
> So, any error/failed login will be displayed using AJAX. But, for
> successful login, I would want it to reload the wholepage, not just
> update the <DIV>.
>
> How can I achieve this?

change the page with javascript.  In the :success callback of your
form_remote_tag, do something like "location.href = 'foo'"



--
rick
http://techno-weenie.net
Vivek K. (Guest)
on 2006-01-05 14:06
(Received via mailing list)
>
>
> change the page with javascript.  In the :success callback of your
> form_remote_tag, do something like "location.href = 'foo'"


But  what about the failure case ?  Should I set a response of HTTP to
something other than 200 in the controller?so that I can handle it via
the
failure callback of the Ajax class in the javascript?
Vivek K. (Guest)
on 2006-01-05 14:54
(Received via mailing list)
ok I got it..

render :nothing => true, :status => 401
Carlos Y. Okada (Guest)
on 2006-01-05 15:00
(Received via mailing list)
Hi Vivek

I think the solution you are looking for is what Jamis B. teached me a
few
weeks ago:
...

When I've needed something like this, I will typically manage things
manually. Instead of specifying the :update element, I will handle
all of that in a :complete handler by hand. The :complete handler
checks the value of a custom header ('X-What-To-Do', for example). If
the header is 'error', then I update the 'ajax_div' element with the
request.responseText data. If the header is 'ok', then I execute
window.location.reload(false).

Does that make sense? If not, let me know and I can post a more
detailed example.

- Jamis
...

here's a snippet from the hypothetical RHTML file:

   <script type="text/javascript">
     function handleComplete(request) {
       var instruction = request.getResponseHeader('X-Instruction')
       if(instruction == "error") {
         $('ajax_div').innerHTML = request.responseText
       } else {
         window.location.reload(false)
       }
     }
   </script>

   <div id="ajax_div"></div>
   <%= form_remote_tag :complete=>'handleComplete(request)',
        :url=>{:controller=>'matters_crud',
               :action =>:update,
               :id => @matter} %>
     <%= render_partial 'form' %>
     <%= submit_tag 'Edit' %>
   </form>

Then, in your controller, you just do:

   def update
     @matter = Matter.find(params[:id])
     if @matter.update_attributes(params[:matter])
       headers['X-Instruction'] = "ok"
       render :nothing => true
     else
       headers['X-Instruction'] = "error"
       render :partial => 'error'
     end
   end

- Jamis
....

Hope that helps

Okada.


2006/1/5, Vivek K. <removed_email_address@domain.invalid>:
Vivek K. (Guest)
on 2006-01-05 15:19
(Received via mailing list)
Hi Carlos,
Thanks for the example..Thats what I was looking for .Just one small
thing.
Whats the false parameter you pass to window.location.reload(). I
actually
want to do something like redirecting to the user's homepage if the
password/username is correct.
So I suppose something like this should work.
document.href='/matters_crud/home'

Vivek
mofo m. (Guest)
on 2006-01-29 17:00
Hi all:

I'm doing something similar to this but for an account signup
controller.  So once the user successfully creates an account, the user
is redirected to index.rhtml by

window.location.href = "http://localhost:3000";

I have a problem where the session hash gets deleted on the first time
through the process, but not deleted on subsequent times.  I'd like to
preserve session[:user_id] from a successful account creation/login, but
it never works the first time.  I can see session[:user_id] = @user.id
being set in irb, but after the redirect session[:user_id] = nil.  And
as previously stated, this works when I create a new account during the
same browser session.

Has anyone else had this problem?

Boram
mofo m. (Guest)
on 2006-01-29 17:02
Clarification:

> as previously stated, this works when I create a new account during the
> same browser session.

should read

> as previously stated, this works when I subsequently create new accounts during > the 
same browser session.
Jonathan V. (Guest)
on 2006-01-30 02:13
I would have thought that RJS provides a much cleaner solution.

update.rjs:

if @matter.valid?
  page << 'location.reload(false);'
else
  page.replace_html 'ajax_div', :partial => 'error'
end

-Jonny.
This topic is locked and can not be replied to.