簡體   English   中英

RABL集合在模板中顯示為空,但在控制器中不顯示

[英]RABL collection appears empty in template but not in controller

我目前正在使用rabl-rails 0.5.3和Rails 4.2.6

我在嘗試從Rails控制器的index方法呈現我的College對象的集合時遇到了這個問題。

這是控制器中index方法的簡化版本:

  def index
    @colleges = College.all

    puts @colleges.count

    render 'api/data_bridge/colleges/show'
  end

我在那里看到puts語句的輸出為“ 100”。 一些@colleges顯然不是空的。

這是rabl索引模板:

puts "---===========================-"
puts @colleges.inspect
puts "---===========================-"
collection @colleges

在服務器輸出中,我看到那些puts語句如下所示:

---===========================-
nil
---===========================-

當我將@colleges轉儲到控制器中時,它肯定不是nil。 我無法弄清楚控制器和模板之間的數據發生了什么。

作為我主張的來源的文檔: https : //github.com/ccocchi/rabl-rails#overview

您的rabl文件應如下所示:

puts "---===========================-"
puts :@colleges.inspect
puts "---===========================-"
collection :@colleges

這是他們的示例:

# app/views/post/index.rabl
collection :@posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }

請密切注意符號前面的“:”。 原因如下:

在任何渲染上下文之外編譯模板的事實使我們無法在模板中使用任何實例變量(節點除外),因為它們是渲染對象。 因此,您必須使用這些變量的符號,例如,要在PostController中呈現集合@posts,您需要在模板內部使用:@posts。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM