yappbox / render_anywhere

Render Rails templates to a string from any class.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Instace variables in templates

justqyx opened this issue · comments

commented

well, while in controller, maybe is

class TestController < ActionCtonroller::Base
def index
  @books = Book.all
end
end

and then,

require 'render_anywhere'
class TestStatic
  def intialize(book)
    @book = book
  end

  def body
    html = render(template: "book/index.html.erb", layout: "application")
    p html
  edn
end

When run, it raise an error sth about @each is a nil class

@justqyx you can do this

rendering_controller.instance_variable_set(:@book, my_book)

which will set the instance variable @book to my_book so that you can write

@book.title

or whatever in your view.

You can also pass in locals

render template: 'book/index', layout: 'application', locals: { :@book => book }