[Rails] Re: Re: Re: calling render_to_string outside of controller
charlie bowman
cbowmanschool at yahoo.com
Sat May 27 18:55:38 GMT 2006
> 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!( "<", "<" )
str.gsub!( ">", ">" )
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
--
Posted via http://www.ruby-forum.com/.
More information about the Rails
mailing list