Rails 渲染助手不渲染

发布于 2024-09-10 21:23:33 字数 863 浏览 4 评论 0原文

我用于渲染部分的辅助函数之一未正确渲染。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-09-17 21:23:33

您的辅助函数正在返回列表@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

在这里,我们用所有部分渲染的片段构建一个字符串,然后返回该字符串(最后提到它)。无论有没有区块,它都可以工作,这一事实纯属巧合。函数的第一行和块的评估结果是相同的。

Your helper function is returning the list @contact_groups. You need to return the partial instead. You might do it like this (rough example):

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文