実現したいこと
商品情報を編集する際、画像が変更できずエラーが発生してしまう。
発生している問題・分からないこと
編集の更新ボタンを押した際、コンソールにて下記エラーメッセージが発生いたします。
エラーメッセージ
error
15:53 2 3 4 GET http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBZ3ciLCJleHAiOm51bGwsInB1ciI6ImJsb2JfaWQifX0=--9927478003bae35be9d79068d20356911a84fd72/bakedcheesecake.jpg 404 (Not Found)
error
1Started PATCH "/items/5" for ::1 at 2024-05-05 17:50:34 +0900 2Processing by ItemsController#update as HTML 3 Parameters: {"authenticity_token"=>"UmC25Xnh+x/1AceM4RYlShbLnCuTIH5irXTH/CJ/cQ/aiOrnJjypcxfKio5YVi+R3iiXb/zSUs+sZM7YEiaQbQ==", "item"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007fce879f7358 @tempfile=#<Tempfile:/var/folders/lz/gh5tmw3x3wx3g7xrsxqsfj4r0000gn/T/RackMultipart20240505-4029-2bc7ad.jpg>, @original_filename="bakedcheesecake.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"item[image]\"; filename=\"bakedcheesecake.jpg\"\r\nContent-Type: image/jpeg\r\n">, "name"=>"ベイクドチーズケーキ", "content"=>"国産素材を使用したチーズケーキです。", "code"=>"222", "category_id"=>"7", "cost"=>"900", "price"=>"1300", "price_unit"=>"箱", "order_unit"=>"1", "quantity"=>"12", "order_lot"=>"1", "eat_time"=>"90"}, "commit"=>"登録する", "id"=>"5"} 4 Item Load (0.7ms) SELECT `items`.* FROM `items` WHERE `items`.`id` = 5 LIMIT 1 5 ↳ app/controllers/items_controller.rb:33:in `update' 6 Rendering items/edit.html.erb within layouts/application 7 Admin Load (0.7ms) SELECT `admins`.* FROM `admins` WHERE `admins`.`id` = 6 ORDER BY `admins`.`id` ASC LIMIT 1 8 ↳ app/views/shared/_header.html.erb:10 9 Rendered shared/_header.html.erb (Duration: 6.5ms | Allocations: 1811) 10 ActiveStorage::Blob Load (0.7ms) SELECT `active_storage_blobs`.* FROM `active_storage_blobs` INNER JOIN `active_storage_attachments` ON `active_storage_blobs`.`id` = `active_storage_attachments`.`blob_id` WHERE `active_storage_attachments`.`record_id` = 5 AND `active_storage_attachments`.`record_type` = 'Item' AND `active_storage_attachments`.`name` = 'image' LIMIT 1 11 ↳ app/views/items/_form.html.erb:12 12 Rendered items/_form.html.erb (Duration: 12.4ms | Allocations: 3468) 13 Rendered items/edit.html.erb within layouts/application (Duration: 19.9ms | Allocations: 5421) 14[Webpacker] Everything's up-to-date. Nothing to do 15Completed 200 OK in 38ms (Views: 29.9ms | ActiveRecord: 2.2ms | Allocations: 12162) 16 17 18Started GET "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBZ3ciLCJleHAiOm51bGwsInB1ciI6ImJsb2JfaWQifX0=--9927478003bae35be9d79068d20356911a84fd72/bakedcheesecake.jpg" for ::1 at 2024-05-05 17:50:34 +0900 19Processing by ActiveStorage::BlobsController#show as JPEG 20 Parameters: {"signed_id"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBZ3ciLCJleHAiOm51bGwsInB1ciI6ImJsb2JfaWQifX0=--9927478003bae35be9d79068d20356911a84fd72", "filename"=>"bakedcheesecake"} 21Filter chain halted as :set_blob rendered or redirected 22Completed 404 Not Found in 2ms (ActiveRecord: 0.0ms | Allocations: 711)
該当のソースコード
items_controller
1class ItemsController < ApplicationController 2 before_action :set_q, only: [:index, :search] 3 def index 4 @items = Item.order(id:"DESC") 5 end 6 7 def new 8 @item = Item.new 9 end 10 11 def create 12 @item = Item.create(item_params) 13 if @item.save 14 redirect_to root_path 15 else 16 render :new 17 end 18 end 19 20 def show 21 @item = Item.find(params[:id]) 22 @total_quantity = @item.stocks.sum(:quantity) 23 @item_stocks = @item.stocks.all 24 @most_old_stocks = @item_stocks.order("eat_limit") 25 @today = Date.today 26 end 27 28 def edit 29 @item = Item.find(params[:id]) 30 end 31 32 def update 33 @item = Item.find(params[:id]) 34 if @item.update(item_params) 35 redirect_to item_path(@item) 36 else 37 render :edit 38 end 39 end 40~~~ 41 private 42 def item_params 43 params.require(:item).permit(:image, :name, :content, :code, :category_id, :cost, :price, :price_unit, :order_unit, :quantity, :order_lot, :eat_time) 44 end 45~~~ 46end
_form.html.erb
1<%= form_with model:item, local: true do |f| %> 2 3 <div class="item-form-field"> 4 <div> 5 <%= f.label :image, "商品写真"%> 6 </div> 7 <div> 8 <div> 9 <%= f.file_field :image, id:"item-edit-image" %> 10 </div> 11 <div> 12 <% if @item.image.attached? %> 13 <%= image_tag @item.image, id: "image-preview", class: "item-img" %> 14 <% else %> 15 <%= image_tag "", id: "iamge-preview", class: "item-img"%> 16 <% end %> 17 </div> 18 </div> 19 </div> 20 21 <div class="item-form-field"> 22 <div> 23 <%= f.label :name, "商品名" %> 24 </div> 25 <div> 26 <%= f.text_area :name, class:"items-text", id:"item-name", placeholder:"商品名(必須 40文字まで)", maxlength:"40" %> 27 </div> 28 </div> 29 30 <div class="item-form-field"> 31 <div> 32 <%= f.label :content, "商品情報" %> 33 </div> 34 <div> 35 <%= f.text_area :content, class:"items-content", id:"item-content", placeholder:"商品の説明(おすすめポイント、使用方法等)を記入する。" ,rows:"7" ,maxlength:"1000" %> 36 </div> 37 </div> 38 39 <div class="item-form-field"> 40 <div> 41 <%= f.label :code, "商品コード" %> 42 </div> 43 <div> 44 <%= f.text_field :code, class:"items-code", id:"item-code", placeholder:"商品コード (例) 0000000" %> 45 </div> 46 </div> 47 48 <div class="item-form-field"> 49 <div> 50 <%= f.label :category_id, "商品カテゴリー" %> 51 </div> 52 <div> 53 <%= f.collection_select(:category_id, Category.all, :id, :name, {}, {class:"select-box", id:"item-category"}) %> 54 </div> 55 </div> 56 57 <div class="item-form-field"> 58 <div> 59 <%= f.label :cost, "商品原価" %> 60 </div> 61 <div> 62 <%= f.text_field :cost, class:"items-cost", id:"item-cost", placeholder:"振替価格 (例)300" %> 63 </div> 64 </div> 65 66 <div class="item-form-field"> 67 <div> 68 <%= f.label :price, "商品販売価格" %> 69 </div> 70 <div> 71 <%= f.text_field :price, class:"items-price", id:"item-price", placeholder:"希望価格 (例) 500" %> 72 </div> 73 </div> 74 75 <div class="item-form-field"> 76 <div> 77 <%= f.label :price_unit, "商品価格単位" %> 78 </div> 79 <div> 80 <%= f.text_area :price_unit, class:"items-price-unit-form", id:"item-price-unit", placeholder:"価格単位 (例)箱(4P入り)" %> 81 </div> 82 </div> 83 84 <div class="item-form-field"> 85 <div> 86 <%= f.label :order_unit, "商品発注単位" %> 87 </div> 88 <div> 89 <%= f.text_field :order_unit, class:"items-order-unit", id:"item-order-unit", placeholder:"最小注文単位 (例)10" %> 90 </div> 91 </div> 92 93 <div class="item-form-field"> 94 <div> 95 <%= f.label :quanitity, "商品ケース入り数" %> 96 </div> 97 <div> 98 <%= f.text_field :quantity, class:"items-quanitity", id:"item-quanitity", placeholder:"ケース入数 (例)12" %> 99 </div> 100 </div> 101 102 <div class="item-form-field"> 103 <div> 104 <%= f.label :order_lot, "商品最小入荷ロット" %> 105 </div> 106 <div> 107 <%= f.text_field :order_lot, class:"items-order-lot", id:"item-order-lot", placeholder:"最小在庫発注単位(ケース) (例)10" %> 108 </div> 109 </div> 110 111 <div class="item-form-field"> 112 <div> 113 <%= f.label :eat_time, "商品賞味期限" %> 114 </div> 115 <div> 116 <%= f.text_field :eat_time, class:"items-eat-time", id:"item-eat-time", placeholder:"賞味期間(日) (例)90" %> 117 </div> 118 </div> 119 120 <div class="item-form-btn"> 121 <%= f.submit "登録する", class:"item-btn"%> 122 </div> 123<% end %>
item.rb/models
1class Item < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 belongs_to :category 4 has_one_attached :image 5 has_many :users, through: :item_users 6 has_many :item_users 7 has_many :admins, through: :item_admins 8 has_many :item_admins 9 has_many :stocks 10 has_many :favorites, dependent: :destroy 11 belongs_to :order 12end
storage.yml
1test: 2 service: Disk 3 root: <%= Rails.root.join("tmp/storage") %> 4 5local: 6 service: Disk 7 root: <%= Rails.root.join("storage") %> 8 9# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) 10# amazon: 11# service: S3 12# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> 13# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> 14# region: us-east-1 15# bucket: your_own_bucket 16 17# Remember not to checkin your GCS keyfile to a repository 18# google: 19# service: GCS 20# project: your_project 21# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> 22# bucket: your_own_bucket 23 24# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) 25# microsoft: 26# service: AzureStorage 27# storage_account_name: your_account_name 28# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> 29# container: your_container_name 30 31# mirror: 32# service: Mirror 33# primary: local 34# mirrors: [ amazon, google, microsoft ]
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
・item_paramsに:imageが記載されているかの確認
・画像をプレビュー表示させることで画像自体が選択できているかの確認
補足
現在の挙動
https://gyazo.com/7e5ac6abb79bd14dc52dc781048e10e1
現在railsを学習の為、他にも記載した方がいいコード等あればコメントいただければ幸いです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー