Rails: Render view from outside controller

前端 未结 8 2144
不思量自难忘°
不思量自难忘° 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:11

    There is no need to over bloat your app with too many gems. As we know ERB is already included in your Rails app.

    @jdf = JDF.new
    @job = ERB.new(File.read(Rails.root + "app/views/entries/job.xml.erb"))
    result =  @job.result(binding)
    

    Above there is snippet of code of an app I'm working on.

    • @jdf is a object to be evaluated in the erb view.
    • In my case I needed to render xml.
    • result is a string to be saved or sent anywhere you like.

提交回复
热议问题