AJAX to submit form, but reload the whole page


#1

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

.

How can I achieve this?


#2

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

.

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


#3

ok I got it…

render :nothing => true, :status => 401


#4

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?


#5

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


#6

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:

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

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:


#7

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


#8

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.


#9

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.