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

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

问题描述

这是一个初学者的铁路问题...



完成后:

  format.xml {head:ok} 

显示视图?如果我在这一点下降函数的结束,我得到我期望的,但如果我调用'返回',我最终在视图(或在我的情况下在一个缺少视图模板)。我可以编写大量的if / else等,但它将是很好的早期的函数,而不会在视图模板中结束。



我搜索过并且不能弄明白这明显的答案是什么;它必须是直接的...

解决方案

你可以使用 render:nothing => true, :status =>:ok 返回没有渲染任何东西,一旦你发送一个渲染:nothing => true你需要从控制器返回,这样的东西可能工作。您可以交换 head()方法调用 render => :nothing 后跟一个 return head() / p>



这里的代码为你做它...





如果无法正确回答您的问题,对于一些有用的用户评论的render调用可以在这里找到:





(对不起,我不能超链接的链接,因为一个新的用户stackoverflow将不允许我发布多个!)


Here's a beginner rails question...

After I do:

format.xml { head: ok}

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...

解决方案

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:

(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天全站免登陆