How to prevent URL change

Hi there,
I do think this is a simple but when we dont know how, it is not simple
at all.
I have got a shopping cart and URL to show it is something like
…/shop/cart/edit, and in the template cart.rhtml, I have some buttons
to increment, decrement, remove items in the cart, accordingly in the
cart controller, I have a method for each button, for instance,

def increment

render :action=>‘edit’
end

It works! However in the browser, the URL is changed from
…/shop/cart/edit to …/shop/cart/increment/[item id]. Then if I click
on refresh button in the browser, the increment action will be redone,
that’s what I want to prevent. What I want exactly is I would like to
see the URL …/shop/cart/edit unchanged after the buttons are clicked.
I know, I can call redirect_to instead of render, but performance would
not be the same. Any idea and hit are greatly appreciated. Thanks!

Franck

On 10 Oct 2007, at 11:17, Franck Yu wrote:

that’s what I want to prevent. What I want exactly is I would like to
see the URL …/shop/cart/edit unchanged after the buttons are
clicked.
I know, I can call redirect_to instead of render, but performance
would
not be the same. Any idea and hit are greatly appreciated. Thanks!

That’s not possible. If the way the browser gets to it is /shop/cart/
increment/[item id] then that’s the url you’ll get, unless you do a
redirect
An alternative would be to use ajax, which won;t change the url and
won’t result in a refresh repeating the action.

Fred

Franck Yu wrote:

Hi there,
I do think this is a simple but when we dont know how, it is not simple
at all.
I have got a shopping cart and URL to show it is something like
…/shop/cart/edit, and in the template cart.rhtml, I have some buttons
to increment, decrement, remove items in the cart, accordingly in the
cart controller, I have a method for each button, for instance,

def increment

render :action=>‘edit’
end

It works! However in the browser, the URL is changed from
…/shop/cart/edit to …/shop/cart/increment/[item id]. Then if I click
on refresh button in the browser, the increment action will be redone,
that’s what I want to prevent. What I want exactly is I would like to
see the URL …/shop/cart/edit unchanged after the buttons are clicked.
I know, I can call redirect_to instead of render, but performance would
not be the same. Any idea and hit are greatly appreciated. Thanks!

Franck

use ajax.

incidentally, i would highly recommend u download (as far as i know the
only one around) the substruct ror commerce code - can learn a lot from
it (code is highly written) as the best way to learn code is to learn
from reading existing projects.

aside from that i’m sure there are parts there u can use in your app
(CTR-c) if u place them in the right spots | modify the code slightly
for u’r specific needs.

either way, it is also a great opportunity to progress into good
programming as well. here:

http://dev.subimage.com/projects/substruct/wiki/InstallingSubstruct

(the code is buried deep in the vendor directory, but it’s not a problem
finding it)

Fred and Shai,
Thank you very much for your reply!
I agree with you: Ajax is the right solution that I will implement in
the following days.

And thank you again for your help!

Franck


Shai R. wrote:

use ajax.

incidentally, i would highly recommend u download (as far as i know the
only one around) the substruct ror commerce code - can learn a lot from
it (code is highly written) as the best way to learn code is to learn
from reading existing projects.

aside from that i’m sure there are parts there u can use in your app
(CTR-c) if u place them in the right spots | modify the code slightly
for u’r specific needs.

either way, it is also a great opportunity to progress into good
programming as well. here:

http://dev.subimage.com/projects/substruct/wiki/InstallingSubstruct

(the code is buried deep in the vendor directory, but it’s not a problem
finding it)

((regarding substruct))

the installation can be a pain, but whatever doesn’t kill ya,
strengthens thee.
worth checking out.

Hello,

FYI, you will need to use redirect_to in conjunction with AJAX to
ensure that you are backwards compatible with non-javascript enabled
browsers.

So, for example:

def increment

redirect_to :action => :edit unless request.xhr?
end

This would fire the edit action fire and display edit view after
increment is executed. This would ensure that the URL does not change.

Additionally, you would have a view called increment.rjs which is the
ruby-javascript code to update the DOM for the AJAX side of things.

Regards,
Jason Arora

On Oct 10, 3:17 am, Franck Yu [email protected]

Hi Jason,

Thank you for your input! It is very helpful! Actually I did not think
about backwards compatibility.

By the way, I found that how to change URL in browser without reloading
the page is a general issue and the only solution seems in AJAX.

Franck
jasoo24 wrote:

Hello,

FYI, you will need to use redirect_to in conjunction with AJAX to
ensure that you are backwards compatible with non-javascript enabled
browsers.

So, for example:

def increment

redirect_to :action => :edit unless request.xhr?
end

This would fire the edit action fire and display edit view after
increment is executed. This would ensure that the URL does not change.

Additionally, you would have a view called increment.rjs which is the
ruby-javascript code to update the DOM for the AJAX side of things.

Regards,
Jason Arora

On Oct 10, 3:17 am, Franck Yu [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs