Rails JSON API仅返回created_at和Updated_at [英] Rails json api only returning created_at and updated_at

查看:78
本文介绍了Rails JSON API仅返回created_at和Updated_at的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开了一个json API来获取自行车数据.该API可以正常工作,因为它确实返回json,但是由于某种原因,它仅返回某些字段,即created_at和updated_at字段.

I have opened a json API to get data on bikes. The API works fine in that it does return json, but for some reason it's only returning certain fields, namely the created_at and updated_at fields.

这是我的数据库中的内容:

Here's what's in my db:

=> #<Bike id: 1, status: "available", location: 0, model: nil, created_at: "2015-02-04 08:21:27", updated_at: "2015-02-04 08:21:27", network_id: nil>

这是我的API返回的内容:

Here's what my API returns:

{"id":1,"created_at":"2015-02-04T08:21:27.183Z","updated_at":"2015-02-04T08:21:27.183Z"}

这是我的控制器代码:

before_action :set_bike, except: [:index, :create]

respond_to :json

def show
  respond_with(@bike)
end

def set_bike
  @bike = Bike.find(params[:id])
end

def bike_params
  params.require(:bike)
  .permit(:status, :model, :network => [:name], :location => [:latitude, :longitude])
end

我的路线默认为json.在我的Bike模型中,没有其他as_json方法.任何想法就是为什么我缺少状态"和位置"

My route has default to json. And in my Bike model there is no additional as_json methods. Any idea's why I'm missing 'status' and 'location'

推荐答案

要调试,您可以使用

def show
  # Temporary debug, to be removed
  pp @bike.to_json

  respond_with(@bike)
end

无论您发现什么,我建议您使用render而不是respond_with来解决问题,例如:

And whatever will be you finding I suggest you to use render instead of respond_with to workaround your issue, like this:

def show
  render json: @bike
end

这篇关于Rails JSON API仅返回created_at和Updated_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆