charlie bowman
2006-May-26 21:55 UTC
[Rails] calling render_to_string outside of controller
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. -- Posted via http://www.ruby-forum.com/.
charlie bowman
2006-May-26 22:25 UTC
[Rails] Re: calling render_to_string outside of controller
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 -- Posted via http://www.ruby-forum.com/.
Ryan Williams
2006-May-26 23:36 UTC
[Rails] Re: calling render_to_string outside of controller
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 On 5/26/06, charlie bowman <cbowmanschool@yahoo.com> wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
charlie bowman
2006-May-27 00:21 UTC
[Rails] Re: Re: calling render_to_string outside of controller
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 Williams 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-- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-May-27 01:18 UTC
[Rails] Re: calling render_to_string outside of controller
Hi! On May 26, 2006, at 3:24 PM, charlie bowman wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsmatch = render_class.send(:render_to_string, {:inline => $1, :type => ''rhtml''}) -Ezra
charlie bowman
2006-May-27 13:07 UTC
[Rails] Re: Re: calling render_to_string outside of controller
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 Zygmuntowicz 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 >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > > match = render_class.send(:render_to_string, {:inline => > $1, :type => ''rhtml''}) > > > -Ezra-- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-May-27 16:47 UTC
[Rails] Re: Re: calling render_to_string outside of controller
Hi ! On May 27, 2006, at 6:07 AM, charlie bowman wrote:> 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 Zygmuntowicz 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 >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> match = render_class.send(:render_to_string, {:inline => >> $1, :type => ''rhtml''}) >> >> >> -EzraCharlie- 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 bowman
2006-May-27 18:55 UTC
[Rails] Re: Re: Re: calling render_to_string outside of controller
> 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- > -EzraEzra, 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/.
Sam Joseph
2006-Jul-14 20:15 UTC
[Rails] Re: Re: calling render_to_string outside of controller
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 charlie bowman wrote:>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 Zygmuntowicz 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 >>>Rails@lists.rubyonrails.org >>>http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> match = render_class.send(:render_to_string, {:inline => >>$1, :type => ''rhtml''}) >> >> >>-Ezra >> >> > > > >
Reasonably Related Threads
- access a ActionControllers protected method from plugin?
- 3.0.0rc ActionController::RoutingError No route matches
- selenium plugin not working with rails 1.1
- RSpec raising routing errors where Rails doesn''t?
- link_to_remote, image_tag, mouseover is this possible?