Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

new smart render misses arguments #607

Closed
lighthouse-import opened this Issue May 16, 2011 · 12 comments

Comments

Projects
None yet
2 participants

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/2358
Created by ippa - 2011-02-22 08:32:33 UTC

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.

Imported from Lighthouse.
Comment by Jeffrey Hardy - 2009-03-27 15:56:59 UTC

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

Imported from Lighthouse.
Comment by thedarkone - 2009-03-27 16:48:36 UTC

You need to call render like this:

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

Imported from Lighthouse.
Comment by ippa - 2009-03-27 17:14:43 UTC

@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?

Imported from Lighthouse.
Comment by Jeffrey Hardy - 2009-03-27 19:39:57 UTC

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/blob/dd2eb1ea7c34eb6496feaf7e42100f37a8dae76b/actionpack/lib/action_view/base.rb#L240

Imported from Lighthouse.
Comment by Jeremy Kemper - 2010-05-04 17:48:44 UTC

[bulk edit]

Imported from Lighthouse.
Comment by Szymon Jeż - 2010-05-16 16:02:14 UTC

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 raised:

undefined method `name' for nil:NilClass

Extracted source (around line #2):

1: 
2:   <%= @organization_unit.name %>
3:   <%= @organization_unit.email %>

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?

Imported from Lighthouse.
Comment by Szymon Jeż - 2010-05-16 16:06:38 UTC

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 raised:

undefined method `name' for nil: NilClass

Extracted source (around line #2):

1: < tr>
2:   < td><%= @organization_unit.name %>
3:   < td><%= @organization_unit.email %>

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?

Imported from Lighthouse.
Comment by iHiD - 2010-08-04 09:08:10 UTC

Did you try:

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

and

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

?

Imported from Lighthouse.
Comment by Szymon Jeż - 2010-08-05 19:17:18 UTC

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.

Imported from Lighthouse.
Comment by Ryan Bigg - 2010-10-09 20:44:52 UTC

Automatic cleanup of spam.

Imported from Lighthouse.
Comment by Dave Schweisguth - 2011-04-04 17:22:09 UTC

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.

Imported from Lighthouse.
Comment by Dave Schweisguth - 2011-04-04 17:29:19 UTC

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.

@josevalim josevalim closed this May 16, 2011

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011

@lifo Bryan Ray + lifo Added options to script/dbconsole to sqlite3 console in various diffe…
…rent modes. [#607 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
f725b19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment