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.

new smart render misses arguments

#2358

The new smart render that automaticly chooses if it's a file, a partial etc seems to miss arguments sometimes. I haven't done extensive research but I've encountered it a couple of times.

This works as expected: <%= render :partial => "open_hours", :locals => { :foo => "bar"} %> ## variable "foo" contains "bar" when accessed in the partial _open_hours.erb

This doesn't: <%= render "open_hours", :locals => { :foo => "bar"} %> ## variable "foo" is undefined when accessed in the partial _open_hours.erb

If I remember it correctly, I encountered the very same problem when rendering a partial with a collection, trying to modify the local variable with the :as option.

Reported by ippa · March 27th, 2009 @ 03:16 PM

State: new
Milestone: 3.x
Assigned to: nobody
Importance: Low

Activity

  1. Jeffrey Hardy
    Jeffrey Hardy

    ActionView::Base#render != ActionController::Base#render. The new, "smart" rendering isn't in AV::Base#render. This confused me too.

    March 27th, 2009 @ 03:56 PM

  2. thedarkone
    thedarkone

    You need to call render like this:

    
    <%= render 'open_hours', :foo => 'bar' %>
    

    March 27th, 2009 @ 04:48 PM

  3. ippa
    ippa

    @Jeffrey: right, but.. both my snippets Does render the correct partial. The confusing part is that the :locals param Only works when I explicity use :partial.

    @thedarkone: Thanks for the shortcut to :locals, but the same "problem" shows itself in others forms too, this is another example:

    <%= render :partial => "kids/kid_presentation", :collection => @kids, :as => :kid %> ## Renders the correct partial, defining the variable "kid" for the items in the collection in the view.

    <%= render "kids/kid_presentation", :collection => @kids, :as => :kid %> ## Renders the correct partial, but the variable "kid" is undefined in the view.

    Shouldn't both work since they both understand that it's a partial?

    March 27th, 2009 @ 05:14 PM

  4. Jeffrey Hardy
    Jeffrey Hardy

    When rendering using a string as the first arg, the second arg is assumed to be the locals hash. So, what you're really getting here is:

    <%= render "kids/kid_presentation", :locals => { :collection => @kids, :as => :kid } %>

    http://github.com/rails/rails/bl...

    March 27th, 2009 @ 07:39 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Szymon Jeż
    Szymon Jeż
    • Tag changed from 2.3.2, render to 2.3.2, 3.x, render

    I have a similar issue like the one described by ippa.
    In my case no variables (locals, objects, collections) passed to render "get" to the partial.
    The only variables that "get" to the partial are the instance variables from the controller action and from the template which is calling the partial to render.
    I tried for several hours (more than 4) many configurations of the render method. Nothing worked. I even tried to dig in to the Rails source code but failed to find the problem.

    I'm using Rails 3.0.0beta3 on Ruby 1.8.7p249.

    I used:

    <%= render :partial => "organization_unit", :collection => @organization_units %>
    <%= render "organization_unit", {:collection => @organization_units, :as => :organization_unit} %>
    <%= render @organization_units %>
    <%= render :partial => 'organization_unit', :locals => {:organization_unit => @organization_unit} %>
    

    etc.

    And I always get:

    NoMethodError in Organization_units#index
    
    Showing /app/views/organization_units/_organization_unit.html.erb where line [#2](/projects/8994/tickets/2 "Ticket #2") raised:
    
    undefined method `name' for nil:NilClass
    
    Extracted source (around line [#2](/projects/8994/tickets/2 "Ticket #2")):
    
    1: 
    
    <%= @organization_unit.name %> <%= @organization_unit.email %>

    May 16th, 2010 @ 05:02 PM

  7. Szymon Jeż
    Szymon Jeż

    Huh? Not all of the comment got posted. It seems that the HTML tags caused some trouble.

    Hire is the continuation of the above comment:

    And I always get:

    NoMethodError in Organization_units#index
    
    Showing /app/views/organization_units/_organization_unit.html.erb where line [#2](/projects/8994/tickets/2 "Ticket #2") raised:
    
    undefined method `name' for nil: NilClass
    
    Extracted source (around line [#2](/projects/8994/tickets/2 "Ticket #2")):
    
    1: < tr>
    2:   < td><%= @organization_unit.name %></ td>
    3:   < td><%= @organization_unit.email %></ td>
    
    Trace of template inclusion: app/views/organization_units/index.html.erb 
    

    Only this code works:

    <% @organization_units.each do |organization_unit| %>
      <% @organization_unit = organization_unit%>
      <%= render "row" %>
    <% end %>
    

    Any clue?

    May 16th, 2010 @ 05:06 PM

  8. iHiD
    iHiD

    Did you try:

    <%= render 'organization_unit', :organization_unit => @organization_unit %>
    

    and

    <% @organization_units.each do |organization_unit| %>
      <%= render "row", :organization_unit => organization_unit %>
    <% end %>
    

    ?

    August 4th, 2010 @ 10:08 AM

  9. Szymon Jeż
    Szymon Jeż

    Today a checked out Rails 3 RC and plyed with my code, this is working:

    <%= render @organization_units %>
    
    and the single organization unit is available as organization_unit local variable in the partial _organization_unit.html.erb.

    I tested this code on Rails 3.0.0.beta4 and 3.0.0.beta3 it's working there to... so actually it was my error [embarrassed]. I used @organization_unit instead of organization_unit in the partial (which was the Rails Way in Rails < 3).

    Sorry for bothering you and IHID - thanks for the suggestion.

    August 5th, 2010 @ 08:17 PM

  10. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:44 PM

  11. bingbing
  12. Dave Schweisguth
    Dave Schweisguth

    This is still a problem. My version:

    render 'foo', :object => bar

    defined a local named foo with the value of bar in Rails 2.3.11. It does not in Rails 3.0.5. A workaround is

    render 'foo', :foo => bar

    but this is ever so slightly duplicative. Is this a regression (which is my guess) or an intentional change? If a regression it would be nice to fix it sooner rather than later and remove one bump in the road from Rails 2 to Rails 3.

    April 4th, 2011 @ 06:22 PM

  13. Dave Schweisguth
    Dave Schweisguth

    P.S. On futher consideration it's possible that the warning box in the section of http://guides.rubyonrails.org/layouts_and_rendering.html documenting :object says that the behavior I was using is intentionally not in Rails 3. (I'm uncertain whether "default local variable" refers to the one set by :object.) Confirmation of that welcome.

    April 4th, 2011 @ 06:29 PM