[英]How to render a different controller / action from within an action in Rails 4.2?
I know this is possible but I want to render a controller / action in response to a different action like in my routes.rb
, I have: 我知道这是可能的,但是我想呈现一个控制器/动作来响应其他动作,例如在
routes.rb
,我有:
get ':handle' => 'handler#index'
and in handler_controller
, I have: 在
handler_controller
,我有:
def index
handle=Handle.where('handle=?',params[:handle]).first
if handle.handle_type=='Location'
# call location / show with id from handle
else
render text: "not found"
end
end
How would I do this? 我该怎么做? I should mention, I don't want to do a redirect.
我应该提到,我不想进行重定向。
Maybe this can help: 也许这可以帮助:
def action_that_calls_one_from_another_controller
controller_you_want = ControllerYouWant.new
controller_you_want.request = request
controller_you_want.response = response
controller_you_want.action_you_want
end
You can solve this by using an "internal redirect" approach. 您可以使用“内部重定向”方法来解决此问题。
Inside rails this can be done via middleware that calls @app
again and tracks redirect loops. 在rails内部,这可以通过再次调用
@app
并跟踪重定向循环的中间件来完成。 See tutorial here 在这里查看教程
Also can be done by utilizing nginx's X-Accel-Redirect
: make a regular routes for referenced resources, but instead of regular redirect issue a X-Accel-Redirect
header, for user it will be like your page rendered without redirect. 也可以通过利用nginx的
X-Accel-Redirect
来完成:为引用的资源建立常规路由,但是不是常规重定向而是发出X-Accel-Redirect
标头,对于用户而言,这就像您的页面无需重定向即可呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.