[英]Rails console shows view is rendered, but doesn't render in browser
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. 我有一个链接,我想对控制器进行ajax调用以删除用户的响应,然后重定向到另一个页面。 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?) (是的,我知道AWS提供了一个错误,但是我假设它与该问题无关?)
EDIT : Thanks to Pamio's answer, this is the solution code for the controller (replacing line 4 above): 编辑 :感谢帕米奥的回答,这是控制器的解决方案代码(替换上面的第4行):
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. 您已使用redirect_to action: "course_page1", format: "html" and return
不正确的内容。 Redirection will work fine if it was an html request but not ajax. 如果重定向是html请求而不是ajax,则重定向将正常工作。 So if you want to redirect despite the fact that the request was an ajax, use may something like this 因此,如果您尽管请求是ajax而仍要重定向,则可以使用如下所示的内容
format.js { render :js => "window.location.href = '#{course_page_path}'" }
Or remove the remote: true
attribute from the link so it would work just fine. 或者从链接中删除remote: true
属性,这样就可以正常工作。
Hope that helps 希望能有所帮助
of course, because you are using ajax, not standard call. 当然,因为您使用的是Ajax,而不是标准调用。 If you want to be redirected, try to remove remote: true
from link_to
. 如果要重定向,请尝试从link_to
删除remote: true
。 Or if you want to keep using ajax, you need by jQuery do redirection from js file. 或者,如果您想继续使用ajax,则需要jQuery从js文件重定向。 In your example it should be in reset_score.js.erb
在您的示例中,它应该在reset_score.js.erb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.