Reputation: 15009
I have a pretty standard Rails HAML new object form that does the usual stuff and ends with:
= f.submit "Add scenario"
This works perfectly, and the scenarios_controller.rb
is also a straightforward:
def create
...create scenario...
redirect_to scenarios_path
end
However, I have also developed a wizard-type form that needs to do some JavaScript on pressing my button:
= f.submit "Add scenario", :onclick => "return validateForm()"
In my CoffeeScript, I collect up all the information and finish up with:
$.post(
url
data
(data, textStatus, jqXHR) ->
# How do I follow the redirect?
return false
My url
and data
are correct as the above controller works correctly and creates my object, but my page doesn't redirect to scenarios_path
. I think I should be doing something with the post result, but searching around the web I cannot find out what, and examining the returned fields in Chrome's debugger doesn't suggest anything. The only suggestion I saw was to use data.redirect
, but such a field doesn't exist, although jqXHR.responseText
seems to contain the page I want to redirect to.
Upvotes: 0
Views: 112
Reputation: 300
I'd treat the HTML call to Scenario#create
and the JS call to Scenario#create
differently, using a respond_to block.
In your scenarios_controller.rb
file:
def create
# create your object
respond_to do |format|
format.html do
redirect_to scenarios_path
end
format.js
end
end
In views/layouts/scenarios/create.js.erb
, then put something like:
window.location.replace("<%= scenarios_path %>");
When you call Scenario#create
with JS, the create.js.erb
file gets rendered and forces the redirect. When you call Scenario#create
with HTML, the redirect_to
call happens as usual.
Upvotes: 2