RABL 集合在模板中显示为空,但在控制器中不显示
RABL collection appears empty in template but not in controller
我目前正在使用 rabl-rails 0.5.3 和 Rails 4.2.6
我遇到了这个问题,我试图从 Rails 控制器的索引方法中呈现我的 College 对象的集合。
这是我的控制器中索引方法的简化版本:
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 转储到控制器中时,它绝对不是零。我想不通控制器和模板之间的数据发生了什么。
作为我声明来源的文档: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) }
注意符号前面的“:”。原因如下:
The fact of compiling the template outside of any rendering context
prevent us to use any instances variables (with the exception of node)
in the template because they are rendering objects. So instead, you'll
have to use symbols of these variables.For example, to render the
collection @posts inside your PostController, you need to use :@posts
inside of the template.
我目前正在使用 rabl-rails 0.5.3 和 Rails 4.2.6
我遇到了这个问题,我试图从 Rails 控制器的索引方法中呈现我的 College 对象的集合。
这是我的控制器中索引方法的简化版本:
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 转储到控制器中时,它绝对不是零。我想不通控制器和模板之间的数据发生了什么。
作为我声明来源的文档: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) }
注意符号前面的“:”。原因如下:
The fact of compiling the template outside of any rendering context prevent us to use any instances variables (with the exception of node) in the template because they are rendering objects. So instead, you'll have to use symbols of these variables.For example, to render the collection @posts inside your PostController, you need to use :@posts inside of the template.