Rails 3.0.7、Windows 7、NetBeans 6.9.1、JRuby 1.5.1、Ruby 1.8.7
我有app/views/browsing/index.rhtml
和这个相关的部分,重点是render
调用:
<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>
我有app/views/browsing/_header_field.rhtml
,内容如下:
<% 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 %>
调试打印(第一行)执行正确,我可以在输出窗口中看到它。我可以使用NetBeans IDE逐行观察这个文件的执行情况,正如预期的那样
问题是,这种呈现的结果以某种方式被丢弃在窗口上,最终呈现的HTML中什么都没有(没有表头单元格)。已渲染(仅相关部分):
<table>
<tr>
<th><em>Actions</em></th>
</tr>
输出窗口生成以下内容:
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)
欢迎所有提示!提前感谢!
是的,<%
仅在<%=
也显示评估代码的情况下进行评估。
您需要更改<% render :partial => 'browsing/header_field', :locals => {:name => name} %>
至<%= render :partial => 'browsing/header_field', :locals => {:name => name} %>