Kyle Macey
Kyle Macey

Reputation: 8154

Chrome Submit form and navigate window in Rails?

So I have an app that submits a form to an external cart using Javascript (target is blank) then I want the window to navigate to a Rails controller to create a small tracker model, controller as such:

def add_to_cart
    @product = Product.find(params[:id])
    @product.cart_trackers.create
    redirect_to :back
end

And the Javascript (in a link_to, onclick helper):

document.forms['addCart#{product.id}'].submit();
window.location.href=\"/products/add_to_cart/#{product.id}\";
return false;

And this works wonderfully in Firefox and Internet Explorer, but Google Chrome throws a fit about so much navigation. I realize AJAX could be a worthy solution, but due to the environment this app runs under (which is a clustercrap of Prototype, Jquery, iFrames, and includes) I'd like to avoid it if possible. Chrome's response is to actually run the window.location... code and skip the form submitting. Without one or the other lines of code, Chrome will perform the single action just fine.

So, what would be the proper way of writing this out? Or should I give in and just wrangle in some AJAX?

Upvotes: 0

Views: 242

Answers (1)

dantswain
dantswain

Reputation: 5467

Try using AJAX-but-not-really-AJAX. Submit the form using whatever AJAX submitter you want (e.g., $.ajax), but don't do anything with the response (i.e., no callback). That way the data gets POSTed and maybe the browser won't be upset that you're asking it to visit two urls back-to-back.

Upvotes: 1

Related Questions