Returning from rails controller

半世苍凉 提交于 2019-12-03 13:48:56

问题


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


回答1:


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:

  • api.rubyonrails.org/classes/ActionController/Base.html#M000635

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

  • gist.github.com/126367

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

  • apidock.com/rails/ActionController/Base/render

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




回答2:


Use

render :nothing => true, :status => :ok

in your action method




回答3:


I guess you must be asking for :

render :nothing => true


来源:https://stackoverflow.com/questions/968799/returning-from-rails-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!