Forum: Ruby on Rails Render a view from console

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Konstantin (Guest)
on 2008-12-18 02:09
(Received via mailing list)
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?
Joshua A. (Guest)
on 2008-12-18 05:56
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?
Konstantin (Guest)
on 2008-12-18 09:28
(Received via mailing list)
> string = ActionController::Base.new.send(:render, :template =>
> 'cont/index')

Unfortunately this doesn't work too :(
>> 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?
This topic is locked and can not be replied to.