Render a view from console


#1

Hello!

In my application I’m trying to render a view from a class in /lib
folder. I have found that it’s very similar to render a view from
console. So, I have tried different methods:

string = ActionView::Base.new.render( :inline => ‘works’, :layout => false )
=> “works”

string = ActionView::Base.new.render( :template => ‘contr/index’, :layout => false )
=> nil

string = ActionView::Base.new.render( :controller => ‘contr’, :action => ‘index’, :layout => false )
=> nil

string = ActionView::Base.new.render( :file => ‘contr/index’, :layout => false )
ActionView::MissingTemplate: Missing template contr/index.html.erb in
view path

string = ActionController::Base.new.render( :template => ‘contr/index’ )
NoMethodError: protected method `render’ called for #

So, nothing works except :inline templates. I saw a similar solution
like ERB.new(“Hello, <%= name %>”).result(binding) - but it’s not
suitable for me because I need to read a template and then insert it
here.

Could you suggest how can I render a template?


#2

I haven’t tried this before, and my suggestion hasn’t been tested but
with the last one, try this:

string = ActionController::Base.new.send(:render, :template =>
‘cont/index’)

AFAIK the send method can be used to call private and protected methods
so this should work.

Hope that helps!

– Josh
http://iammrjoshua.com

Konstantin wrote:

Hello!

In my application I’m trying to render a view from a class in /lib
folder. I have found that it’s very similar to render a view from
console. So, I have tried different methods:

string = ActionView::Base.new.render( :inline => ‘works’, :layout => false )
=> “works”

string = ActionView::Base.new.render( :template => ‘contr/index’, :layout => false )
=> nil

string = ActionView::Base.new.render( :controller => ‘contr’, :action => ‘index’, :layout => false )
=> nil

string = ActionView::Base.new.render( :file => ‘contr/index’, :layout => false )
ActionView::MissingTemplate: Missing template contr/index.html.erb in
view path

string = ActionController::Base.new.render( :template => ‘contr/index’ )
NoMethodError: protected method `render’ called for #

So, nothing works except :inline templates. I saw a similar solution
like ERB.new(“Hello, <%= name %>”).result(binding) - but it’s not
suitable for me because I need to read a template and then insert it
here.

Could you suggest how can I render a template?


#3

string = ActionController::Base.new.send(:render, :template =>
‘cont/index’)

Unfortunately this doesn’t work too :frowning:

ActionController::Base.new.send(:render, :template => ‘cont/index’, :layout => false)
NoMethodError: You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.render_file

ActionController::Base.new.send(:render, :controller => ‘cont’, :action => ‘index’, :layout => false)
NoMethodError: You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.render_file

Do you have more suggestions?