Search code examples
ruby-on-railsruby-on-rails-3renderpartial

Partial rendering not shown


Rails 3.0.7, Windows 7, NetBeans 6.9.1, JRuby 1.5.1, Ruby 1.8.7

I have app/views/browsing/index.rhtml with this relevant part, focus on the render call:

<table>
  <tr>
    <th><em><%=I18n.t('browsing.actions')%></em></th>
    <% colnames = @_controller.model_class.column_names %>
    <% colnames.each do |name| %>
    <% render  :partial => 'browsing/header_field', :locals => {:name => name} %>
    <% end %>
  </tr>

And I have app/views/browsing/_header_field.rhtml with the following content:

<% puts "DEBUG: rendering " + name + " field." %>
<% if @_controller.orderables.include?(name.to_sym) %>
<%
  otext = ''
  oopts = @_controller.orderings[name];
  if !oopts.nil?
    otext += ' ' + (oopts[:is_asc] ? '↓' : '↑')
    otext += '<span class="small">'+oopts[:prio].to_s+'</span>'
  end
%>
<th><%=link_to( name.tr('_',' '), { :controller => controller_path, :action => 'index', :o => name }, {:class => 'sort'} )%>
<%=((otext=='' || @_controller.orderings.size<=1) ? raw(otext) : link_to(raw(otext), { :controller => controller_path, :action => 'index', :o => name, :x => true }, {:class => 'delete_sort', :title => I18n.t('browsing.delete_sort')}))%></th>
<% else %>
<th><%=name.tr('_',' ')%></th>
<% end %>

The debug-print (first line) is executed properly, I can see it in the output window. I can use the NetBeans IDE to watch the execution of this file line by line, it happens as it is expected

The problem is that the results of this rendering are somehow dropped out on the window, there is nothing (no table header cells) in the final rendered HTML. That is rendered (only relevant part):

<table>
  <tr>
    <th><em>Actions</em></th>
  </tr>

The output window produces this:

DEBUG: rendering id field.
DEBUG: rendering inm_device_id field.
DEBUG: rendering changes_id field.

Started GET "/changes_and_devices" for 127.0.0.1 at Wed Jun 08 11:50:01 +0200 2011
  Processing by ChangesAndDevicesController#index as HTML
  ChangeAndDevice Load (2.0ms)  SELECT `changes_and_devices`.* FROM `changes_and_devices` ORDER BY id desc LIMIT 20 OFFSET 0
  SQL (18.0ms)  SELECT COUNT(*) FROM `changes_and_devices`
Rendered browsing/_header_field.rhtml (6.0ms)
Rendered browsing/_header_field.rhtml (6.0ms)
Rendered browsing/_header_field.rhtml (5.0ms)
Rendered browsing/index.rhtml within layouts/application (189.0ms)
Completed 200 OK in 245ms (Views: 207.0ms | ActiveRecord: 20.0ms)

All hints are welcome! Thanks in advance!


Solution

  • Yep, <% only evaluates where <%= also displays evaluated code.

    You need to change
    <% render :partial => 'browsing/header_field', :locals => {:name => name} %> to
    <%= render :partial => 'browsing/header_field', :locals => {:name => name} %>