フロントエンドはVue、バックエンドはRails で実装を進めています。
Vue から複数の画像をActive Storageに保存した際、各画像をrails_blob_url
してimagesの配列に格納した状態でapiに反映させたいのですが、jbuilderの書き方がいまいち分からず困っています。
どのように書けば良いのかご教示お願い致します。よろしくお願い致します。
has_one_attached :image
で1枚で保存出来るようにした時はrails_blob_url
を使用して、URLを取得出来ています。
ruby
1app/views/api/designs/show.json.jbuilder 2 3 json.extract! @design, :id, :title, :description, :nail_part 4 json.image rails_blob_url(@design.image) if @design.image.attached?
ruby
1http://localhost:3000/api/designs/1 2 3{ 4 id: 1, 5 title: "title1", 6 description: "description1", 7 nail_part: "hand", 8 image: "http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsi/dog.png" 9}
has_many_attached :images
にして複数画像を保存できるようにした場合の試した書き方が以下です。
試してみた書き方1
ruby
1json.extract! @design, :id, :title, :description, :nail_part 2 3json.array! @design.images do |image| 4 json.images [rails_blob_url(image)] 5end
↓エラーになる。
ruby
1Can't merge [{"images"=>["http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--27e2f8064adb107762817c40b91bdaa3cc60f85c/2022-08-15%2013-41-38%20UTC"]}, {"images"=>["http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e00459b6810aa1166ac9b74d57bd16e1e31c9262/2022-08-15%2013-41-38%20UTC"]}] into {"id"=>11, "title"=>"test", "description"=>"test", "nail_part"=>"hand"}
試してみた書き方2
ruby
1json.array! @design.images do |image| 2 json.extract! @design, :id, :title, :description, :nail_part 3 json.images [rails_blob_url(image)] 4end
↓分かれてしまう。
ruby
1[ 2{ 3id: 11, 4title: "test", 5description: "test", 6nail_part: "hand", 7images: [ 8"http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--27e2f8064adb107762817c40b91bdaa3cc60f85c/2022-08-15%2013-41-38%20UTC" 9] 10}, 11{ 12id: 11, 13title: "test", 14description: "test", 15nail_part: "hand", 16images: [ 17"http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e00459b6810aa1166ac9b74d57bd16e1e31c9262/2022-08-15%2013-41-38%20UTC" 18] 19} 20]
試してみた書き方3
ruby
1json.extract! @design, :id, :title, :description, :nail_part 2@design.images.map do |image| 3 json.images [].push(rails_blob_url(image)) 4end
↓1枚しか格納されない。
ruby
1{ 2id: 11, 3title: "test", 4description: "test", 5nail_part: "hand", 6images: [ 7"http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e00459b6810aa1166ac9b74d57bd16e1e31c9262/2022-08-15%2013-41-38%20UTC" 8] 9}
理想
ruby
1{ 2id: 11, 3title: "test", 4description: "test", 5nail_part: "hand", 6images: [ 7"http://localhost:3000/rails/active_storage/blobs/redirect/1枚目","http://localhost:3000/rails/active_storage/blobs/redirect/2枚目" 8] 9}
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。