Rails 3 render action from another controller

后端 未结 4 2054
攒了一身酷
攒了一身酷 2020-12-01 08:19

I need to render another controller action <%= render \"controller/index\" %> and i get this error

Missing partial controller/inde

4条回答
  •  不知归路
    2020-12-01 08:20

    This works well for me :

    def renderActionInOtherController(controller,action,params)
      controller.class_eval{
        def params=(params); @params = params end
        def params; @params end
      }
      c = controller.new
      c.request = @_request
      c.response = @_response
      c.params = params
      c.send(action)
      c.response.body
    end
    

    then, call by

    render :text => renderActionInOtherController(OtherController,:otherAction,params)
    

    basically it hacks the other class and overwrites its "params" method and return

    If you are using Rails 4:

    def renderActionInOtherController(controller,action,params)
        c = controller.new
        c.params = params
        c.dispatch(action, request)
        c.response.body
    end
    

提交回复
热议问题