我正在尝试使用RABL呈现一个非常简单的数据结构,但我无法弄清楚如何正确删除子根节点.这是我的两个模板.
首先是集合索引模板.
collection @groups, :object_root => false
attributes :id, :name
child :files do
extends 'groups/_file'
end
Run Code Online (Sandbox Code Playgroud)
接下来,文件部分模板.
object @file
attributes :id
Run Code Online (Sandbox Code Playgroud)
这两个模板最终生成以下JSON:
[
{
"id":"4f57bf67f85544e620000001",
"name":"Some Group",
"files":[
{
"file":{
"id":"4f5aa3fef855441009000007"
}
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我想找到一种方法来删除文件集合中的根"文件"键.就像是:
[
{
"id":"4f57bf67f85544e620000001",
"name":"Some Group",
"files":[
{
"id":"4f5aa3fef855441009000007"
}
]
}
]
Run Code Online (Sandbox Code Playgroud) 我使用devise的authenticate_user!控制器中的方法.当请求中提供的auth_token是正确的,但如果身份验证失败,我最终得到的结果是:
curl -XGET 'http://localhost:3000/my_obj?auth_token=wrongtoken'
<html><body>You are being <a href="http://localhost:3000/users/sign_in">redirected</a>.</body></html>
Run Code Online (Sandbox Code Playgroud)
当我使用rabl时,有什么是最好的方法
{'error' : 'authentication error'}
Run Code Online (Sandbox Code Playgroud)
返回html重定向的intead?
我想要一个Rails控制器(所有这些,实际上,它是一个API)来永远呈现JSON.
我不希望Rails返回"找不到路由",或者尝试找不到HTML模板,或者返回406.我只是希望它自动并始终呈现JSON,例如从RABL或JBuilder视图.
这可能吗?相关问题似乎有答案,具有上述缺点.
我有一个ruby哈希,我想用RABL渲染.哈希看起来像这样:
@my_hash = {
:c => {
:d => "e"
}
}
Run Code Online (Sandbox Code Playgroud)
我试图用一些RABL代码渲染它:
object @my_hash => :some_object
attributes :d
node(:c) { |n| n[:d] }
Run Code Online (Sandbox Code Playgroud)
但我收到了 {"c":null}
如何使用RABL渲染?
我有这个模板:
# app/views/posts/index.rabl
collection @posts => :posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }
Run Code Online (Sandbox Code Playgroud)
女巫回归:
{
"posts": [
{
"post": {
"id": 5,
"title": "...",
"subject": "...",
"user": {
"full_name": "..."
},
"read": true
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想添加一些分页参数来呈现这个:
{
"posts": [
{
"post": {
"id": 5,
"title": "...",
"subject": "...",
"user": {
"full_name": "..."
},
"read": true
}
}
],
"total": 42,
"total_pages": 12
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?非常感谢!
要为大规模应用程序构建api,哪种方法是更好的性能,我应该使用Rabl,Jbuilder还是手动构建json对象?我正在为移动应用程序构建api /端点.
我正在尝试做这样的事情:
cache "api/v1/cars_index/#{I18n.locale}/car/#{car.cache_key}" do
attributes :id, :brand, :model_name, :fuel, :km, :year, :price
node(:color) { |car| car.color.present? ? car.color : '' }
end
Run Code Online (Sandbox Code Playgroud) 我正试图在一个轨道3.2 rake任务中渲染一个rabl视图.我将它呈现为字符串,以便通过Pusher从后台任务发送一些JSON.我从rake任务答案中查看了各种render_to_string,但它们似乎都没有工作.这是我到目前为止:
controller = PostsController.new
av = ActionView::Base.new(MyApp::Application.config.paths['app/views'].first,{},controller)
@post = post
Pusher["some channel"].trigger('new_post', av.render(:template => 'posts/show.json.rabl'))
Run Code Online (Sandbox Code Playgroud)
通过这次尝试,我获得了一个ActionView :: Template :: Error异常和错误"未定义的方法`参数'为nil:NilClass".
Rabl允许您通过在视图中命名属性来获取属性,例如:
object @user
attributes :name, :email
Run Code Online (Sandbox Code Playgroud)
我有一个模型,其属性将不知道,但我想使用rabl显示从我的实例变量中的控制器返回的所有内容.
有没有像这样的快捷方式:
attributes :all
Run Code Online (Sandbox Code Playgroud)
等等
谢谢
我必须遵循rabl代码来生成一些JSON数据.
object @event
attributes :id, :EID, :name, :address, :description, :latitude, :longitude, :time, :created_at
node(:rsvp_count) { |event| event.rsvp_users.count }
node(:check_in_count) { |event| event.checkedin_users.count }
node(:FID) { |event| event.creater.FID if event.creater}
child :rsvp_users, :object_root => false do
extends 'users/index'
end
child :checkedin_users, :object_root => false do
extends 'users/index'
end
Run Code Online (Sandbox Code Playgroud)
它生成的数据如下所示:
[
{
"event": {
"id": 2,
"EID": 123458,
"name": "event no.2",
"address": "189 elm st",
"description": "awesome event",
"latitude": 10,
"longitude": 10,
"time": "2013-10-20T18:00:00Z",
"created_at": "2013-08-15T21:06:21Z",
"rsvp_count": 3,
"check_in_count": 0,
"FID": 12345678, …Run Code Online (Sandbox Code Playgroud)