从 rails 控制器返回 [英] Returning from rails controller

查看:29
本文介绍了从 rails 控制器返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个初学者 Rails 问题...

Here's a beginner rails question...

在我这样做之后:

format.xml { head: ok}

如何从控制器端点返回而不显示视图?如果我此时退出函数的末尾,我会得到我期望的结果,但是如果我调用返回",我会在视图中结束(或者在我的情况下是在缺少的视图模板中).我可以编写很多 if/else 等代码,但最好早点退出函数而不用在视图模板中结束.

How do I return from the controller endpoint without showing the view? If I drop off the end of the function at this point, I get what I expect, but if I call 'return', I end up in the view (or in my case in a missing view template). I can code up lots of if/else etc., but it would be nice to early out from the function without ending up in a view template.

我四处搜索,但无法弄清楚明显的答案是什么;它必须很简单...

I've searched around and can't figure out what the obvious answer is to this; it must be straightforward...

推荐答案

你可以使用 "render :nothing => true, :status => :ok" 返回而不渲染任何东西,一旦你发送了一个渲染 :nothing => true 你需要从控制器返回,这样的事情可能会起作用.您可以将 head() 方法调用替换为 render =>;:nothing 后跟一个 returnhead() 方法记录在这里:

You can use "render :nothing => true, :status => :ok" to return without rendering anything, once you have send a render :nothing => true you need to return from the controller, something like this might work. You can swap the head() method call for a render => :nothing followed by a return, the head() method is documented here:

这是应该为你做的代码......

Here's the code that should do it for you...

如果这不能正确回答您的问题,请联系我,可以在此处找到包含一些有用的用户评论的渲染调用文档:

Ping me if that doesn't properly answer your question, documentation for the render call with some helpful user comments can be found here:

(抱歉,我无法为您提供超链接,因为新用户 stackoverflow 不允许我发布多个链接!)

(sorry I couldn't hyperlink the links for you, as a new user stackoverflow won't allow me to post more than one!)

这篇关于从 rails 控制器返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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