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

前端 未结 10 1789
不思量自难忘°
不思量自难忘° 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:36

    You can use ActionView directly and render partials to string without having a controller. I find that pattern useful to create models that encapsulate some javascript generation, for instance.

    html = ActionView::Base.new(Rails.configuration.paths['app/views']).render(
      partial: 'test', 
      formats: [:html],
      handlers: [:erb],
      locals: { variable: 'value' }
    )
    

    Then, just put your _test.html.erb in you view folder and try it out!

提交回复
热议问题