Rails, How to render a view/partial in a model

前端 未结 10 1820
不思量自难忘°
不思量自难忘° 2020-12-22 18:55

In my model I have:

after_create :push_create

I push_create I need to render a view. I\'m trying to do that like so:

  def         


        
10条回答
  •  盖世英雄少女心
    2020-12-22 19:26

    The render methods are defined on the ActiveController class and its progeny. Inherently you do not have access to it on the model, nor is it a class method so you can't use it without an instance of the controller.

    I've never tried to instantiate a controller for the express purpose of simply stringifying a partial, but if you can get your hands on a controller, render_to_string seems to be the way to go.

    I will chime in by saying that if you're going down this path you're taking RoR "off the Rails". This is a violation of MVC and fundamentally poor program design.This doesn't mean I think you're a bad person :P Sometimes life drives us off the rails, so to speak.

    I can't speak to the details that have driven you to do this, but I'd strongly suggest you rethink your approach.

提交回复
热议问题