前提・実現したいこと
railsでチャットサイトを作ろうとしているのですが、
画像をActiveStorageで保存することはできたのですがAWSのS3に保存しようとするとエラーが起きます。
環境変数のキーを確認しても間違いは無かったです。
NoMethodError (undefined method `upload' for nil:NilClass):
のエラー文で調べてみてもS3に関しての事は見当たりませんでした。
発生している問題・エラーメッセージ
NoMethodError (undefined method `upload' for nil:NilClass):
エラーログ Started POST "/rooms/9/messages" for ::1 at 2020-09-02 12:58:10 +0900 Processing by MessagesController#create as HTML Parameters: {"authenticity_token"=>"hxr4QGPTl+qIMc1OALw81hgeGtmbfOk1IyweMpdDH5nXP6T2515XAKEkL0kYCMSnapMV9rIP/x4NTWmEIz4QOw==", "message"=>{"comment"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007faeca6336e0 @tempfile=#<Tempfile:/var/folders/x2/c8_sxg453438sw3wvwgzdvnc0000gn/T/RackMultipart20200902-45765-16i050p.jpg>, @original_filename="lotte_icon.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"message[image]\"; filename=\"lotte_icon.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"送信", "room_id"=>"9"} User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1 Room Load (0.3ms) SELECT `rooms`.* FROM `rooms` WHERE `rooms`.`id` = 9 LIMIT 1 ↳ app/controllers/messages_controller.rb:11:in `create' (0.1ms) BEGIN ↳ app/controllers/messages_controller.rb:13:in `create' User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 ↳ app/controllers/messages_controller.rb:13:in `create' Message Create (0.3ms) INSERT INTO `messages` (`comment`, `room_id`, `user_id`, `created_at`, `updated_at`) VALUES ('', 9, 2, '2020-09-02 03:58:10.671587', '2020-09-02 03:58:10.671587') ↳ app/controllers/messages_controller.rb:13:in `create' ActiveStorage::Attachment Load (0.3ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 6 AND `active_storage_attachments`.`record_type` = 'Message' AND `active_storage_attachments`.`name` = 'image' LIMIT 1 ↳ app/controllers/messages_controller.rb:13:in `create' ActiveStorage::Blob Create (0.2ms) INSERT INTO `active_storage_blobs` (`key`, `filename`, `content_type`, `metadata`, `byte_size`, `checksum`, `created_at`) VALUES ('pf4ojs60s6jodz0ge77jyw1r2072', 'lotte_icon.jpg', 'image/jpeg', '{\"identified\":true}', 366946, 'SZ/da66RHgi8N2Q4OhFDEw==', '2020-09-02 03:58:10') ↳ app/controllers/messages_controller.rb:13:in `create' ActiveStorage::Attachment Create (0.2ms) INSERT INTO `active_storage_attachments` (`name`, `record_type`, `record_id`, `blob_id`, `created_at`) VALUES ('image', 'Message', 6, 5, '2020-09-02 03:58:10') ↳ app/controllers/messages_controller.rb:13:in `create' Message Update (0.2ms) UPDATE `messages` SET `messages`.`updated_at` = '2020-09-02 03:58:10.678384' WHERE `messages`.`id` = 6 ↳ app/controllers/messages_controller.rb:13:in `create' (0.8ms) COMMIT ↳ app/controllers/messages_controller.rb:13:in `create' Completed 500 Internal Server Error in 20ms (ActiveRecord: 3.0ms | Allocations: 13290) NoMethodError (undefined method `upload' for nil:NilClass): app/controllers/messages_controller.rb:13:in `create'
該当のソースコード
yml
1storage.yml 2 3test: 4 service: Disk 5 root: <%= Rails.root.join("tmp/storage") %> 6 7local: 8 service: Disk 9 root: <%= Rails.root.join("storage") %> 10 11amazon: 12 service: S3 13 access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %> 14 secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %> 15 region: ap-northeast-1 16 bucket: baseball-chat2020
ruby
1development.rb 2config.active_storage.service = :amazon 3
ruby
1rooms_controller.rb 2 3class RoomsController < ApplicationController 4 before_action :authenticate_user!, only: [:create] 5 def index 6 @room = Room.new 7 @rooms = Room.all.order(id: "DESC") 8 end 9 def create 10 @room = Room.new(room_params) 11 if @room.save #<---ここでエラーが出る 12 redirect_to root_path 13 else 14 render :index 15 end 16 end 17 def destroy 18 room = Room.find(params[:id]) 19 room.destroy 20 redirect_to root_path 21 end 22 23 private 24 def room_params 25 params.require(:room).permit(:name,:content,:official).merge(user_id: current_user.id) 26 end 27end 28
補足情報(FW/ツールのバージョンなど)
rails 6.0.0
回答1件
あなたの回答
tips
プレビュー