Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

render_to_string is completely broken

#296
class MyController < ApplicationController
  def index	 
    @x = render_to_string :controller => 'i_do_not_exists'
  end
end

There is no exception raised if I use :controller which does not exist at all. Whatever options I use, @x always contains app/views/my/index.html.erb file content (even for existing controllers render_to_string ignore every parameter)

Reported by Jaroslaw Zabiello · June 2nd, 2008 @ 09:12 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Pratik
    Pratik
    • State changed from new to incomplete

    Use render_to_string :template => 'i_do_not_exists/index'

    What is the behavior in Rails 2.0.x ?

    June 2nd, 2008 @ 12:18 PM

  2. Jaroslaw Zabiello
    Jaroslaw Zabiello

    I do NOT want to render just another template (without its action). I want to render another action. I have just checked and I see it is broken for RoR 2.0.2 and 2.1.0 as well. Therefore information displayed in api.rubyonrails.org is misleading and wrong (capitalization is mine):

    render_to_string(options = nil, &block) 
    
    Renders according to THE SAME RULES AS RENDER, but returns the result in a string instead of sending it as the response body to the browser. 
    
    # app/views/controllers/my_controller.rb:
    
    class MyController < ApplicationController
      def index 
        @slot = render_to_string :action => 'second'
      end
      
      def second    
        @var = 'World'
        render :text => 'It will never be displayed from host/my!' 
      end
    end
           
    
    # app/views/my/index.html.erb:
    
    Slot: <%= @slot %> 
             
    # app/views/my/second.html.erb:
    
    Hello <%= @msg %>! 
    
    

    Result at http://localhost:3000/my:

    Result:

    Slot: Hello !
    

    The @msg variable was not rendered at all. render :text was also ignored at all. It looks like render_to_string ignores Ruby code in the action.

    June 2nd, 2008 @ 01:46 PM

  3. Pratik
    Pratik
    • State changed from incomplete to invalid

    You cannot render another action. render_to_string is only for rendering templates. It will not call action code.

    June 2nd, 2008 @ 01:49 PM