Rails: Render view from outside controller

前端 未结 8 2177
不思量自难忘°
不思量自难忘° 2020-12-15 03:41

I\'m trying to create an HTML string using a view. I would like to render this from a class that is not a controller. How can I use the rails rendering engine outside a co

8条回答
  •  情深已故
    2020-12-15 04:15

    Calling directly render method on ApplicationController may raise error

     Helper Devise: could not find the `Warden::Proxy` instance on request environment
    

    Instead we can use ApplicationController.renderer.render like

     rendered_string = ApplicationController.renderer.render(
         partial: 'users/show',
         locals: { user: user }
    )
    

提交回复
热议问题