如何在Rails 4.2中的动作中呈现不同的控制器/动作?

时间:2016-01-29 22:29:20

标签: ruby-on-rails

我知道这是可能的,但我想渲染一个控制器/动作以响应我routes.rb中的不同动作,我有:

get ':handle' => 'handler#index'

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

我该怎么做?我应该提一下,我不想做重定向。

2 个答案:

答案 0 :(得分:0)

也许这会有所帮助:

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

答案 1 :(得分:0)

您可以使用“内部重定向”方法解决此问题。

内部轨道可以通过再次调用@app并跟踪重定向循环的中间件来完成。请参阅教程here

也可以通过利用nginx的X-Accel-Redirect:为引用的资源制作常规路由,但不是常规重定向发出X-Accel-Redirect标头,对于用户而言,它就像您的页面呈现而没有重定向。