Rails 渲染助手不渲染
我用于渲染部分的辅助函数之一未正确渲染。
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>
我完全注释掉了 block 函数,它仍然显示上面的内容。
我做错了什么?
编辑:我刚刚意识到 ## 来自 @contact_groups,它是页面上分配的最后一个值。它返回值并且不渲染块内的任何代码。
One of my helper functions to render partials is not rendering correctly.
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
All that displays on the page is
##
When looking at the HTML of the rendered page, it looks like this:
#<ContactGroup:0x103090c78>#<ContactGroup:0x103090b60>
I commented out the block function completely and it still displayed the above.
What am I doing wrong?
Edit: I just realized that the ## is coming from @contact_groups being the last value assigned on the page. It is returning the value and not rendering any of the code within the block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的辅助函数正在返回列表
@contact_groups
。您需要返回部分内容。您可以这样做(粗略的示例):在这里,我们用所有部分渲染的片段构建一个字符串,然后返回该字符串(最后提到它)。无论有没有区块,它都可以工作,这一事实纯属巧合。函数的第一行和块的评估结果是相同的。
Your helper function is returning the list
@contact_groups
. You need to return the partial instead. You might do it like this (rough example):Here, we build a string with all the partially-rendered pieces, then return that string (by mentioning it last). The fact that it worked with and without the block is a coincidence; both the first line of your function and the block evaluate to the same thing.