![](/img/trans.png)
[英]My rails helper is rendering '<' as '<'! How to make it render correctly?
[英]Rails Render Helper Not Rendering
我的帮助器功能之一是无法部分渲染。
def render_contact_list
@contact_groups = ContactGroup.find(:all, :conditions => ["user_id = ?", current_user.id])
@contact_groups.each do |contact_group|
@contacts = Contact.find(:all, :conditions => ["group_id = ? and Owner_id = ?", contact_group.id, current_user.id])
render :text => "#{contact_group.name}<br />"
render(:partial => 'contacts/partials/view_contacts', :collection => @contacts, :as => :contact)
end
end
页面上显示的全部是
##
查看呈现页面的HTML时,它看起来像这样:
#<ContactGroup:0x103090c78>#<ContactGroup:0x103090b60>
我完全注释了块功能,它仍然显示以上内容。
我究竟做错了什么?
编辑 :我刚刚意识到##来自@contact_groups是页面上分配的最后一个值。 它正在返回该值,而不呈现该块内的任何代码。
您的辅助函数将返回列表@contact_groups
。 您需要返回部分。 您可以这样做(一个粗略的例子):
def render_contact_list
@contact_groups = ContactGroup.find(:all, :conditions => ["user_id = ?", current_user.id])
text = ""
@contact_groups.each do |contact_group|
@contacts = Contact.find(:all, :conditions => ["group_id = ? and Owner_id = ?", contact_group.id, current_user.id])
text << render :text => "#{contact_group.name}<br />"
text << render(:partial => 'contacts/partials/view_contacts', :collection => @contacts, :as => :contact)
end
text
end
在这里,我们用所有部分渲染的片段构建一个字符串,然后返回该字符串(最后提及)。 它在有无块的情况下都起作用的事实是一个巧合; 函数的第一行和该块的计算结果相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.