rails 没有加载 JSON 中的所有属性
rails doesn't load all attributes in JSON
我使用 Rails 作为后端,使用 React 作为前端。
我通过Carrierwave to my AWS S3 bucket上传吃饭的照片。
当我这样做时,这是我从 rails 控制台得到的:
m = Meal.first
[...]
m.photo
=> #<PhotoUploader:0x007fef8e4381b0 @model=#<Meal id: 1, name: "Burger Caviar", price: "5.99", original_price: "8.90", description: "Delicieux Burger, parfume au caviar d'aubergine et...", ingredients: "Pain, steack de boeuf, caviar, salade, tomates, oi...", allergenes: "aucun", category: "Français", week_day: 1, photo: "Photo_May_21_11_45_12_PM_1024x1024.jpg", vacation_mode: false, restaurant_id: 1, created_at: "2018-11-06 13:00:46", updated_at: "2018-11-06 13:00:46">, @mounted_as=:photo, @file=#<CarrierWave::Storage::Fog::File:0x007fef8e433c50 @uploader=#<PhotoUploader:0x007fef8e4381b0 ...>, @base=#<CarrierWave::Storage::Fog:0x007fef8e433ea8 @uploader=#<PhotoUploader:0x007fef8e4381b0 ...>>, @path="uploads/Photo_May_21_11_45_12_PM_1024x1024.jpg", @content_type=nil>, @filename=nil, @cache_id=nil, @versions={}, @storage=#<CarrierWave::Storage::Fog:0x007fef8e433ea8 @uploader=#<PhotoUploader:0x007fef8e4381b0 ...>>>
m.photo.path
=> "uploads/Photo_May_21_11_45_12_PM_1024x1024.jpg"
m.photo.url
=> "https://take-a-meal-images.s3.amazonaws.com/uploads/Photo_May_21_11_45_12_PM_1024x1024.jpg"
我实际上需要 m.photo.path
将我的图像(通过 imgix)提供给 React 中的客户端。
然而,当我通过 axios 和 console.log
回复我的饭菜时,我得到的只是照片 URL:
data: Array(9)
[...]
0:
created_at: "2018-11-06T14:00:49.373+01:00"
id: 10
name: "Burger Nordiste"
photo:
url: "https://take-a-meal-images.s3.amazonaws.com/uploads/header-image.jpg"
[...]
我的controllers/meals_controller.rb
:
class MealsController < ApplicationController
def index
@meals = Meal.all
render json: @meals.to_json(include: :restaurant)
end
end
那么为什么 rails 只服务于 photo.url
,我该如何更改它以便在 React 中访问 photo.path
?
覆盖上传器默认序列化程序的最简单解决方案是在上传器中重载 serializable_hash
方法。
If this is an overload you need to apply often I would recommend creating a module/concern to avoid duplication.
# app/uploaders/photo_uploader.rb
class PhotoUploader < CarrierWave::Uploader::Base
# existing code...
def serializable_hash(*args)
super(*args).merge({'path': path})
end
end
我使用 Rails 作为后端,使用 React 作为前端。
我通过Carrierwave to my AWS S3 bucket上传吃饭的照片。
当我这样做时,这是我从 rails 控制台得到的:
m = Meal.first
[...]
m.photo
=> #<PhotoUploader:0x007fef8e4381b0 @model=#<Meal id: 1, name: "Burger Caviar", price: "5.99", original_price: "8.90", description: "Delicieux Burger, parfume au caviar d'aubergine et...", ingredients: "Pain, steack de boeuf, caviar, salade, tomates, oi...", allergenes: "aucun", category: "Français", week_day: 1, photo: "Photo_May_21_11_45_12_PM_1024x1024.jpg", vacation_mode: false, restaurant_id: 1, created_at: "2018-11-06 13:00:46", updated_at: "2018-11-06 13:00:46">, @mounted_as=:photo, @file=#<CarrierWave::Storage::Fog::File:0x007fef8e433c50 @uploader=#<PhotoUploader:0x007fef8e4381b0 ...>, @base=#<CarrierWave::Storage::Fog:0x007fef8e433ea8 @uploader=#<PhotoUploader:0x007fef8e4381b0 ...>>, @path="uploads/Photo_May_21_11_45_12_PM_1024x1024.jpg", @content_type=nil>, @filename=nil, @cache_id=nil, @versions={}, @storage=#<CarrierWave::Storage::Fog:0x007fef8e433ea8 @uploader=#<PhotoUploader:0x007fef8e4381b0 ...>>>
m.photo.path
=> "uploads/Photo_May_21_11_45_12_PM_1024x1024.jpg"
m.photo.url
=> "https://take-a-meal-images.s3.amazonaws.com/uploads/Photo_May_21_11_45_12_PM_1024x1024.jpg"
我实际上需要 m.photo.path
将我的图像(通过 imgix)提供给 React 中的客户端。
然而,当我通过 axios 和 console.log
回复我的饭菜时,我得到的只是照片 URL:
data: Array(9)
[...]
0:
created_at: "2018-11-06T14:00:49.373+01:00"
id: 10
name: "Burger Nordiste"
photo:
url: "https://take-a-meal-images.s3.amazonaws.com/uploads/header-image.jpg"
[...]
我的controllers/meals_controller.rb
:
class MealsController < ApplicationController
def index
@meals = Meal.all
render json: @meals.to_json(include: :restaurant)
end
end
那么为什么 rails 只服务于 photo.url
,我该如何更改它以便在 React 中访问 photo.path
?
覆盖上传器默认序列化程序的最简单解决方案是在上传器中重载 serializable_hash
方法。
If this is an overload you need to apply often I would recommend creating a module/concern to avoid duplication.
# app/uploaders/photo_uploader.rb
class PhotoUploader < CarrierWave::Uploader::Base
# existing code...
def serializable_hash(*args)
super(*args).merge({'path': path})
end
end