This project is archived and is in readonly mode.
new smart render misses arguments
Reported by ippa | March 27th, 2009 @ 03:16 PM | in 3.x
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.
Comments and changes to this ticket
-
Jeffrey Hardy March 27th, 2009 @ 03:56 PM
ActionView::Base#render != ActionController::Base#render. The new, "smart" rendering isn't in AV::Base#render. This confused me too.
-
thedarkone March 27th, 2009 @ 04:48 PM
You need to call render like this:
<%= render 'open_hours', :foo => 'bar' %>
-
ippa March 27th, 2009 @ 05:14 PM
@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?
-
Jeffrey Hardy March 27th, 2009 @ 07:39 PM
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 } %>
-
Szymon Jeż May 16th, 2010 @ 05:02 PM
- 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 %> -
Szymon Jeż May 16th, 2010 @ 05:06 PM
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?
-
iHiD August 4th, 2010 @ 10:08 AM
Did you try:
<%= render 'organization_unit', :organization_unit => @organization_unit %>
and
<% @organization_units.each do |organization_unit| %> <%= render "row", :organization_unit => organization_unit %> <% end %>
?
-
Szymon Jeż August 5th, 2010 @ 08:17 PM
Today a checked out Rails 3 RC and plyed with my code, this is working:
<%= render @organization_units %>
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 oforganization_unit
in the partial (which was the Rails Way in Rails < 3).Sorry for bothering you and IHID - thanks for the suggestion.
-
Ryan Bigg October 9th, 2010 @ 09:44 PM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
-
Dave Schweisguth April 4th, 2011 @ 06:22 PM
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.
-
Dave Schweisguth April 4th, 2011 @ 06:29 PM
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>