Forum: Ruby on Rails calling render_to_string outside of controller

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.
Charlie B. (Guest)
on 2006-05-27 01:46
How can I call render_to_string outside of a controller.  I'm stuck on
this.  I've nearly got my plugin finished but I can't seem to get this
to work.  The method is in ActionController::Base but it is protected.
Charlie B. (Guest)
on 2006-05-27 02:24
charlie bowman wrote:
> How can I call render_to_string outside of a controller.  I'm stuck on
> this.  I've nearly got my plugin finished but I can't seem to get this
> to work.  The method is in ActionController::Base but it is protected.

I'm going to keep posting my attempts, maybe someone out there knows how
to do this.  Here is the offending portion on code.  It almost workd but
I get this:

protected method `render_to_string' called for
#<ActionController::Base:0xb7850f14>

def execute_ruby_code( str )
  render_class = ActionController::Base.new
  logger.error('test')
  logger.error(render_class.class)
  str = str.gsub(/\<ruby\>(.*?)\<\/ruby\>/) do |match|
    match = render_class.render_to_string(:inline => $1, :type =>
'rhtml')
  logger.error(match)
  end
  str
end
Ryan W. (Guest)
on 2006-05-27 03:37
(Received via mailing list)
This is untested, but you could get around the protection by doing

match = render_class.instance_eval {render_to_string(:inline => $1,
:type =>
'rhtml')}

-RYaN
Charlie B. (Guest)
on 2006-05-27 04:20
Thank you for your help! It got me past the protected method error, but
it appears that I'm still missing something.  At line 992 there is
@assigns, it appears that this array is filled with the parameters from
the request header.  Any clues on how to get around this.  Again thank
you for your help!

 NoMethodError in Admin/postController#preview

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]=

vendor/rails/actionpack/lib/action_controller/base.rb:992:in
`add_class_variables_to_assigns'
vendor/rails/actionpack/lib/action_controller/base.rb:991:in
`add_class_variables_to_assigns'

Ryan W. wrote:
> This is untested, but you could get around the protection by doing
>
> match = render_class.instance_eval {render_to_string(:inline => $1,
> :type =>
> 'rhtml')}
>
> -RYaN
Ezra Z. (Guest)
on 2006-05-27 05:18
(Received via mailing list)
Hi!

On May 26, 2006, at 3:24 PM, charlie bowman wrote:

> to do this.  Here is the offending portion on code.  It almost
>   str = str.gsub(/\<ruby\>(.*?)\<\/ruby\>/) do |match|
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails



    match = render_class.send(:render_to_string, {:inline =>
$1, :type => 'rhtml'})


-Ezra
Charlie B. (Guest)
on 2006-05-27 17:07
Thank you for your help! This method works just at the previous, but I'm
still getting the same nil object error.  I decided to try something a
little different and I managed to get a little further.  The code below
works for everything I've tried(which isn't that much) but it breaks if
the template string has a link_to inside of it.  The link to method
would be the most common bit of could I'm looking to use.  I think when
the plugin is called I don't have the proper response object.   At least
that's my theory.

Here is my error and code

NoMethodError (You have a nil object when you didn't expect it!
The error occured while evaluating nil.url_for):
    .//vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:27:in
`url_for'
    .//vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:59:in
`link_to'
    compiled-template:1:in `_run_rhtml_2'
    .//vendor/rails/actionpack/lib/action_view/base.rb:314:in
`compile_and_render_template'
    .//vendor/rails/actionpack/lib/action_view/base.rb:290:in
`render_template'
    .//vendor/plugins/acts_as_blog/lib/acts_as_blog.rb:54:in
`execute_ruby_code'


def execute_ruby_code( str )
  render_class = ActionView::Base.new
  logger.error('test')
  logger.error(render_class.class)
  str = str.gsub(/\<ruby\>(.*?)\<\/ruby\>/) do |match|
    match = render_class.render_template('rhtml',$1)
   end
   logger.error(str)
   str
end


Ezra Z. wrote:
> Hi!
>
> On May 26, 2006, at 3:24 PM, charlie bowman wrote:
>
>> to do this.  Here is the offending portion on code.  It almost
>>   str = str.gsub(/\<ruby\>(.*?)\<\/ruby\>/) do |match|
>> Rails mailing list
>> removed_email_address@domain.invalid
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
>     match = render_class.send(:render_to_string, {:inline =>
> $1, :type => 'rhtml'})
>
>
> -Ezra
Ezra Z. (Guest)
on 2006-05-27 20:48
(Received via mailing list)
Hi !

On May 27, 2006, at 6:07 AM, charlie bowman wrote:

> the plugin is called I don't have the proper response object.   At
>     .//vendor/rails/actionpack/lib/action_view/helpers/
>
>
>>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>>     match = render_class.send(:render_to_string, {:inline =>
>> $1, :type => 'rhtml'})
>>
>>
>> -Ezra


Charlie-

	Maybe you can paste a little more code from your plugin? I'm a
little unclear exactly what you are trying to do. So maybe if you can
explain the problem you are trying to solve I can help?


Cheers-
-Ezra
Charlie B. (Guest)
on 2006-05-27 22:55
> Charlie-
>
> 	Maybe you can paste a little more code from your plugin? I'm a
> little unclear exactly what you are trying to do. So maybe if you can
> explain the problem you are trying to solve I can help?
>
>
> Cheers-
> -Ezra

Ezra,
  Thanks for taking the time to help!

The plugin I'm working on is going to allow me to embed ruby code into a
blog post and have it executed when it is retrieved or possibly before
it is saved.  Here is an example of a blog post

the time is <ruby><%= Time.now %></ruby>

The above is currently working with the plugin.  The following will not
it gives the error I mention above.

Here is my <ruby><%= link_to('link',:controller => 'foo', :action =>
'bar')%></ruby>

The plugin is called acts_as_blog. Here is the code from
lib/acts_as_blog.rb

require 'active_record'

## Lets create a namespace
module TextConversion
        ## make it acts_as_blog
        module Acts
                module Blog

      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def acts_as_blog
          class_eval do
            extend TextConversion::Acts::Blog::SingletonMethods
          end
        end
      end

       module SingletonMethods
        def convert_to_html(txt, text_filter, restrictions = [])
                      return "" if txt.blank?
                  return txt if text_filter.blank?

                  text_filter.split.each do |filter|
                  case filter
                  when "markdown":
                          txt = BlueCloth.new(txt, restrictions).to_html
                        when "textile":
                          txt = self.encode_html(txt) if
restrictions.include?(:filter_html)
                                                        txt =
execute_ruby_code(txt) unless restrictions.include?(:filter_html)
                          txt = RedCloth.new(txt,
restrictions).to_html(:textile)
                          txt.gsub!('\n','<br />')
                        when "smartypants":
                   txt = RubyPants.new(txt).to_html
                  end
                        end
                return txt
                end
                                def execute_ruby_code( str )
                                        render_class =
ActionView::Base.new
                                        logger.error('test')
                                        logger.error(render_class.class)
                                        str =
str.gsub(/\<ruby\>(.*?)\<\/ruby\>/) do |match|
                                                match =
render_class.render_template('rhtml',$1)
                                                #logger.error(match)
                                        end
                                        logger.error(str)
                                        str
                                end

                                # Taken from BlueCloth since RedCloth's
filter_html is broken
                def self.encode_html( str )
                str.gsub!( "<", "&lt;" )
                str.gsub!( ">", "&gt;" )
                str
                end
        # etc...
      end

      end

    end
  end

# reopen ActiveRecord and include all the above to make
# them available to all our models if they want it

ActiveRecord::Base.class_eval do
  include TextConversion::Acts::Blog
end
Sam J. (Guest)
on 2006-07-15 00:17
(Received via mailing list)
Hi Charlie,

Did you ever fully resolve this?

I tried the "render_class.render_template('rhtml',$1)" but get the
protected method error, and "render_class.send(:render_to_string,
{:inline =>
$1, :type => 'rhtml'})" screws up when it hits the class assigns.

I created a patch for handling the link_to error that occurs when one
uses that controller method in mail templates:

http://dev.rubyonrails.org/ticket/3517

which might help you here.

Currently I'm trying to use render_to_string in the selenese template
handler for the Selenium on Rails testing framework, so that we can
include ruby in our selenese scripts.

What I'd really like is be able to call more than one template handler
for a given file type ...

CHEERS> SAM
This topic is locked and can not be replied to.