This seems very weird. I have a link that I want to make an ajax call to the controller to delete a users' responses, then redirect to another page. Here's the code for the link:
<%= link_to 'try again', reset_score_path, remote: true, controller: "pages",
action: "reset_score", class: "btn btn-primary tryAgain", data: { confirm: 'This
will reset your score to <strong>zero</strong> and reset all of your answers so you
can start fresh. Do you want to continue?', commit: "Restart", cancel: "No Thanks",
focus: 'none'}, title: "Try Again?" %>
And here's the controller action:
def reset_score
current_user.responses.destroy_all
current_user.certificate.destroy
redirect_to action: "course_page1", format: "html" and return
end
But the problem is that nothing happens in the browser; it doesn't redirect. But the console shows that it is processing and rendering the view. See console output:
Started GET "/pages/reset_score" for 127.0.0.1 at 2014-10-17 07:19:45 -0500
Processing by PagesController#reset_score as JS
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 62 ORDER BY "users"."id" ASC LIMIT 1
Response Load (0.3ms) SELECT "responses".* FROM "responses" WHERE "responses"."user_id" = ? [["user_id", 62]]
[AWS S3 404 0.306706 0 retries] head_object(:bucket_name=>"...",:key=>"x.pdf") AWS::S3::Errors::NoSuchKey No Such Key
Redirected to http://localhost:3000/pages/course_page1.html
Completed 302 Found in 315ms (ActiveRecord: 0.6ms)
Started GET "/pages/course_page1.html" for 127.0.0.1 at 2014-10-17 07:19:45 -0500
Processing by PagesController#course_page1 as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 62 ORDER BY "users"."id" ASC LIMIT 1
Rendered pages/course_page1.html.erb within layouts/application (0.3ms)
Rendered layouts/_header.html.erb (0.4ms)
Completed 200 OK in 80ms (Views: 78.5ms | ActiveRecord: 0.2ms)
So it appears to show that the view is rendered. However the browser does not actually show this view - it just stays on the previous page.
Any ideas? I'm stumped!
(yes I know AWS is giving an error, but I'm assuming its unrelated to this problem?)
EDIT: Thanks to Pamio's answer, this is the solution code for the controller (replacing line 4 above):
def reset_score
current_user.responses.destroy_all
current_user.certificate.destroy
respond_to do |format|
format.js { render :js => "window.location.href = '#{pages_course_page1_path}'" }
end
end
You have used
redirect_to action: "course_page1", format: "html" and return
which is not right. Redirection will work fine if it was an html request but not ajax. So if you want to redirect despite the fact that the request was an ajax, use may something like thisformat.js { render :js => "window.location.href = '#{course_page_path}'" }
Or remove the
remote: true
attribute from the link so it would work just fine.Hope that helps
of course, because you are using ajax, not standard call. If you want to be redirected, try to remove
remote: true
fromlink_to
. Or if you want to keep using ajax, you need by jQuery do redirection from js file. In your example it should be inreset_score.js.erb