re: 13.1 & 14.1 render_to_string troubles Hi, I''m trying to use render_to_string to render a partial and then pass the rendered html along to a view in a variable (I really need to store the item in a DB, but let''s not complicate things now ;). I''ve noticed the following odd behavior. Here''s the setup: ''_foo.rhtml'' This is foo.rhtml, I''m a partial ''index.rhtml'' In ''index.rhtml'' here comes the string: <%= @output_string %> ''hello_controller.rb'': class HelloController < ApplicationController def index @output_string = render_to_string :partial => ''foo'' #this doesn''t seem to work end end You''d expect to see ''This is foo.rhtml, I''m a partial'' in the output, but oddly enough this doesn''t seem to work. If we do this: # @output_string = render_to_string :partial => ''foo'' #this doesn''t seem to work @output_string = "this will be rendered instead of the foo partial" Then index.rhtml renders the expected string, "this will be rendered....". And here''s the kicker, this is interesting: @output_string = render_to_string :partial => ''foo'' #this doesn''t seem to work @output_string = "this will be rendered instead of the foo partial" _Doesn''t_ render anything. You''d expect the second assign to work and see "this will be rendered...", but instead no output happens. Odd no? Thoughts? Is this a bug or is there some magic I''m missing? Thanks, Sanford
Ok, so here''s a shorter version of the problem: Controller Action ================def rts_test @foo = render_to_string :inline => "this is a test" render :layout => false end rts_test.rhtml ================The value of foo is: ::<%= @foo %>:: Reults: ================= Wanted: "The value of foo is: ::this is a test:: Got : "The value of foo is: ::::" Is this a bug or am I missing some magic? Sanford SV wrote:> re: 13.1 & 14.1 render_to_string troubles > > Hi, I''m trying to use render_to_string to render a partial and then pass > the rendered html along to a view in a variable (I really need to store > the item in a DB, but let''s not complicate things now ;). > > I''ve noticed the following odd behavior. Here''s the setup: > > ''_foo.rhtml'' > This is foo.rhtml, I''m a partial > > ''index.rhtml'' > In ''index.rhtml'' here comes the string: <%= @output_string %> > > ''hello_controller.rb'': > class HelloController < ApplicationController > def index > > @output_string = render_to_string :partial => ''foo'' #this doesn''t > seem to work > end > end > > > You''d expect to see ''This is foo.rhtml, I''m a partial'' in the output, > but oddly enough this doesn''t seem to work. > > If we do this: > > # @output_string = render_to_string :partial => ''foo'' #this doesn''t > seem to work > @output_string = "this will be rendered instead of the foo partial" > > Then index.rhtml renders the expected string, "this will be rendered....". > > And here''s the kicker, this is interesting: > > @output_string = render_to_string :partial => ''foo'' #this doesn''t > seem to work > @output_string = "this will be rendered instead of the foo partial" > > _Doesn''t_ render anything. You''d expect the second assign to work and > see "this will be rendered...", but instead no output happens. Odd no? > > Thoughts? Is this a bug or is there some magic I''m missing? > > Thanks, > > Sanford
Just a guess: Have you defined foo as an attribute with one of: attr_* :foo I don''t think this is required, but it''s worth a try. -- -- Tom Mornini On Oct 21, 2005, at 5:44 PM, Sanford SV wrote:> Ok, so here''s a shorter version of the problem: > > Controller Action > ================> def rts_test > @foo = render_to_string :inline => "this is a test" > render :layout => false > end > > rts_test.rhtml > ================> The value of foo is: ::<%= @foo %>:: > > Reults: > =================> > Wanted: "The value of foo is: ::this is a test:: > > Got : "The value of foo is: ::::" > > > Is this a bug or am I missing some magic? > > > > Sanford SV wrote: > >> re: 13.1 & 14.1 render_to_string troubles >> Hi, I''m trying to use render_to_string to render a partial and >> then pass the rendered html along to a view in a variable (I >> really need to store the item in a DB, but let''s not complicate >> things now ;). >> I''ve noticed the following odd behavior. Here''s the setup: >> ''_foo.rhtml'' >> This is foo.rhtml, I''m a partial >> ''index.rhtml'' >> In ''index.rhtml'' here comes the string: <%= @output_string %> >> ''hello_controller.rb'': >> class HelloController < ApplicationController >> def index >> @output_string = render_to_string :partial => ''foo'' #this >> doesn''t seem to work >> end >> end >> You''d expect to see ''This is foo.rhtml, I''m a partial'' in the >> output, but oddly enough this doesn''t seem to work. >> If we do this: >> # @output_string = render_to_string :partial => ''foo'' #this >> doesn''t seem to work >> @output_string = "this will be rendered instead of the foo >> partial" >> Then index.rhtml renders the expected string, "this will be >> rendered....". >> And here''s the kicker, this is interesting: >> @output_string = render_to_string :partial => ''foo'' #this >> doesn''t seem to work >> @output_string = "this will be rendered instead of the foo >> partial" >> _Doesn''t_ render anything. You''d expect the second assign to work >> and see "this will be rendered...", but instead no output >> happens. Odd no? >> Thoughts? Is this a bug or is there some magic I''m missing? >> Thanks, >> Sanford >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Saturday 22 October 2005 02:44, Sanford SV wrote:> Ok, so here''s a shorter version of the problem: > > Controller Action > ================> def rts_test > @foo = render_to_string :inline => "this is a test" > render :layout => false > end > > rts_test.rhtml > ================> The value of foo is: ::<%= @foo %>:: > > Reults: > =================> > Wanted: "The value of foo is: ::this is a test:: > > Got : "The value of foo is: ::::" > > > Is this a bug or am I missing some magic? >Yesterday I found something similar after updating to 0.14. First, try in controller @foo = "this is a test". If it works, you can use this Controller: def index @output_partial = ''foo'' end Template In ''index.rhtml'' here comes the string: <%= render :partial => @output_partial %> That worked for me. Anyway this seems to be a weird bug. Cheers, Koke -- Personal: koke-Ss5wYDU21Mf9CHvZ6l3MoQ@public.gmane.org :: koke-LHjXKMxSopRAfugRpC6u6w@public.gmane.org Jabber: koke-ev9KT9qnfNfs60DWnuRNLA@public.gmane.org Blog: http://koke.amedias.org/ "Aquel que recibe de mi una idea se instruye a si mismo sin disminuir mi educacion; como aquel que encienda su vela en la mia, recibe luz sin ensombrecerme." -- Thomas Jefferson, 1813
Sanford SV wrote:>> >> _Doesn''t_ render anything. You''d expect the second assign to work >> and see "this will be rendered...", but instead no output happens. >> Odd no? >> >> Thoughts? Is this a bug or is there some magic I''m missing? >> >> Thanks, >> >> Sanford > >It''s a bug. Will be fixed. If you have a .tgz or .zip install you can replace function render_to_string in ActionController::Base with: def render_to_string(options = nil) #:doc: result = render(options) erase_render_results @variables_added = nil @template.instance_variable_set("@assigns_added", nil) result end Or just add it to your application.rb controller file. -- stefan
Thanks Stefan! Stefan Kaes wrote:> Sanford SV wrote: > >>> >>> _Doesn''t_ render anything. You''d expect the second assign to work >>> and see "this will be rendered...", but instead no output happens. >>> Odd no? >>> >>> Thoughts? Is this a bug or is there some magic I''m missing? >>> >>> Thanks, >>> >>> Sanford >> >> >> > It''s a bug. Will be fixed. If you have a .tgz or .zip install you can > replace function render_to_string in ActionController::Base with: > > def render_to_string(options = nil) #:doc: > result = render(options) > erase_render_results > @variables_added = nil > @template.instance_variable_set("@assigns_added", nil) > result > end > Or just add it to your application.rb controller file. > > -- stefan