Rails的控制台显示视图渲染,但不呈现在浏览器 [英] Rails console shows view is rendered, but doesn't render in browser

查看:241
本文介绍了Rails的控制台显示视图渲染,但不呈现在浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很奇怪。我有我想要调用Ajax控制器删除用户的反应,然后重定向到另一个页面的链接。这里的code的链接:

 <%=的link_to再试一次,reset_score_path,遥控器:真,控制器:网页,
动作:reset_score类:BTN BTN-主要TRYAGAIN,数据:{证实:这
将重置你的分数到<强>零< / STRONG>并重置你的答案,所以你
可以重新开始。 ?你想继续,承诺:重新启动,取消:不,谢谢,
重点:'无'},标题:再试一次 %>
 

和这里的控制器操作:

 高清reset_score
  current_user.responses.destroy_all
  current_user.certificate.destroy
  redirect_to时的动作:course_page1,格式为:HTML和返回
结束
 

但问题是,没有任何反应,在浏览器;它不重定向。但是控制台显示它正在处理和渲染视图。参见控制台输出:

 入门使用/页/ reset_score为127.0.0.1,在2014年10月17日七时19分45秒-0500
处理由PagesController#reset_score为JS
用户负载(0.3ms)选择用户。*从用户WHERE用户,ID= 62 ORDER BY用户,IDASC LIMIT 1
响应负载(0.3ms)选择响应。* FROM响应WHERE回应。user_ID的=? [user_ID的,62]
[AWS S3 404 0.306706 0重] head_object(:bucket_name =>中...,:键=>中x.pdf)AWS :: S3 ::错误:: NoSuchKey没有密钥

重定向到http://本地主机:3000 /页/ course_page1.html
完成302发现在315ms(ActiveRecord的:0.6ms)

入门使用/pages/course_page1.html为127.0.0.1,在2014年10月17日7时19分45秒-0500
处理由PagesController#course_page1为HTML
用户负载(为0.2ms),选择用户。*从用户WHERE用户,ID= 62 ORDER BY用户,IDASC LIMIT 1
渲染页/布局内course_page1.html.erb /应用程序(0.3ms)
渲染布局/ _header.html.erb(0.4ms)
完成200确定在80毫秒(浏览次数:78.5ms | ActiveRecord的:为0.2ms)
 

因此​​它似乎表明视图显示。然而,浏览器实际上并没有表现出这种观点 - 它只是停留在previous页

任何想法?我难倒!

(是的,我知道AWS是给一个错误,但我假设它无关的这个问题吗?)

修改: 由于Pamio的答案,这是解决方案code的控制器(取代上述4号线):

 高清reset_score
    current_user.responses.destroy_all
    current_user.certificate.destroy

    respond_to代码做|格式|
        format.js {渲染:JS => window.location.href ='#{pages_course_page1_path}}
    结束
结束
 

解决方案

您已经使用 redirect_to时的动作:course_page1,格式为:HTML,返回这是不对。重定向将正常工作,如果它是一个HTML请求,但没有AJAX。所以,如果你想重定向尽管请求是一个AJAX,可以使用这样的事情

format.js {渲染:JS => window.location.href ='#{course_page_path}}

或删除远程:真正的链接属性,所以它会工作得很好。

希望帮助

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 this

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.

Hope that helps

这篇关于Rails的控制台显示视图渲染,但不呈现在浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆