前提
Active Storageを使用してrailsに保存した複数の画像データをVueに渡すにあたり、Jbuilderの書き方で分からない点があります。
やりたいこととしては以下になります。
理想
javascript
1 2images: [ 3 { 4 id: 1, 5 url: "http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHM/test1.jpeg" 6 }, 7 { 8 id: 2, 9 url: "http://localhost:3000/rails/active_storage/blobs/redirect/OnsibWVzc2Fn/test2.jpeg" 10 }, 11]
現状、複数画像のurlは以下のようにmapで回すと取得できます。
ruby
1urls = @school.images.map { |image| rails_blob_url(image) } 2json.images urls if @school.images.attached?
↓
urls: [ "http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHM/test1.jpeg", "http://localhost:3000/rails/active_storage/blobs/redirect/OnsibWVzc2Fn/test2.jpeg" ],
eachで回した時に取得できる、以下の値の中に上記の配列をそれぞれ組み込みたいのですが、書き方がよく分からず困っています。
ruby
1image = @school.images.each { |image| image.id } 2json.images image if @school.images.attached?
↓
ruby
1images: [ 2 { 3 id: 1, 4 name: "images", 5 record_type: "School", 6 record_id: 1, 7 blob_id: 1, 8 created_at: "2022-09-24T15:53:50.385+09:00" 9 }, 10 { 11 id: 2, 12 name: "images", 13 record_type: "School", 14 record_id: 1, 15 blob_id: 2, 16 created_at: "2022-09-24T15:53:50.408+09:00" 17 } 18], 19
set! merge! 等を使用してみたのですが、うまく行きません。
ご教示いただけますと幸いです。
よろしくお願いします。
あなたの回答
tips
プレビュー