現状
railsで作成したアプリをAWSの設定やcapistranoで自動デプロイした状態です。
本番環境で投稿を行うと下記エラーが発生いたしました。
We're sorry, but something went wrong. If you are the application owner check the logs for more information.
なお、S3のバケットには画像が入っており、
開発環境では画像は表示できていませんが投稿はできる状態です。
リファレンスやその他記事などを参考に対処しましたが解決できず、
申し訳ございませんが質問させて頂きます。
実現したいこと
本番環境でも投稿ができる。
開発環境・Gem
開発環境
- Rails 5.2.4.2
- ruby 2.5.1
- AWS(EC2,S3)
- Application Server Unicorn
- Web Server Nginx
Gem
- carrierwave
- mini_magick
- fog-aws
- capistrano
対応したこと
- EC2含め再起動を実行。
修正した際は「unicorn・Nginx・MySQL」を再起動させた後に、
bundle exec cap production deployを実行しました。
development.log
でエラーログの確認
ログを確認すると下記内容が記載されており指摘を受けたコードはconfig.storage = :fog
だった。
DEPRECATION WARNING: #fog_provider is deprecated and has no effect (called from block in <top (required)> at /Users/***/+++/***-app/config/initializers/carrierwave.rb:7)
carrierwaveに関する指摘があったのでGit HubやWeb記事などを
参考にコードを照らし合わせて修正するも状況は変わらず。
併せて、 carrierwave
のGemをversionせずにbundle installするも解決できず。
コード
[config/initializers/carrierwave.rb] require 'carrierwave/storage/abstract' require 'carrierwave/storage/file' require 'carrierwave/storage/fog' CarrierWave.configure do |config| if Rails.env.production? config.storage = :fog config.fog_provider = 'fog/aws' config.fog_credentials = { provider: 'AWS', aws_access_key_id: Rails.application.credentials.aws[:access_key_id], aws_secret_access_key: Rails.application.credentials.aws[:secret_access_key], region: 'ap-northeast-1' } config.fog_directory = '#S3のバケット名' config.asset_host = 'https://s3-ap-northeast-1.amazonaws.com/bucket_name' else config.storage :file config.enable_processing = false if Rails.env.test? end end
追記
[cat production.log] D, [2020-04-24T07:59:26.562168 #8911] DEBUG -- : [a457333a-4c4b-4770-96b6-41913fda344a] User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 I, [2020-04-24T07:59:26.902848 #8911] INFO -- : [a457333a-4c4b-4770-96b6-41913fda344a] Completed 500 Internal Server Error in 342ms (ActiveRecord: 0.3ms) F, [2020-04-24T07:59:26.903563 #8911] FATAL -- : [a457333a-4c4b-4770-96b6-41913fda344a] F, [2020-04-24T07:59:26.903604 #8911] FATAL -- : [a457333a-4c4b-4770-96b6-41913fda344a] ActiveModel::UnknownAttributeError (unknown attribute 'user_id' for Post.): F, [2020-04-24T07:59:26.903674 #8911] FATAL -- : [a457333a-4c4b-4770-96b6-41913fda344a] F, [2020-04-24T07:59:26.903721 #8911] FATAL -- : [a457333a-4c4b-4770-96b6-41913fda344a] app/controllers/posts_controller.rb:15:in `create'
[posts_controller] class PostsController < ApplicationController before_action :move_to_index, except: [:index, :show] before_action :set_post, only: [:show, :destroy, :edit, :update] def index @posts = Post.all.limit(5).order("created_at DESC") end def new @post = Post.new end def create @post = Post.new(post_params) if @post.save redirect_to root_path else render :new end end def show end def destroy if @post.destroy redirect_to root_path else render post_path(post.user_id,post.id) end end def edit end def update if @post.update(post_update_params) redirect_to post_path(@post) else render :edit end end def post_params params.require(:post).permit( :title, :category_id, :image, :pharse, :summery).merge(user_id: current_user.id) end def post_update_params params.require(:post).permit( :title, :category_id, :image, :image_cache ,:remove_image, :pharse, :summery).merge(user_id: current_user.id) end def move_to_index redirect_to root_path unless user_signed_in? end def set_post @post = Post.find(params[:id]) end end
[new.html.haml] =render partial: "header" .post__container .post__container__content .post__container__content__text %h1 テキスト .post__container__content__input =form_with(model: @post, local:true) do |f| .post__container__content__input__form .title = f.label :title, "テキスト", class: 'input-content' %span.form-require テキスト %br/ = f.text_field :title, autofocus: true, placeholder: "テキスト", class: 'content-default', id: "src_img" .category = f.label :category_id, "カテゴリー", class: 'input-content' %span.form-require テキスト %br/ =f.collection_select :category_id, Category.all,:id,:name, {prompt: '選択してください'}, class: 'content-default' .image = f.label :image, "テキスト", class: 'input-content' %span.form-require テキスト %br/ #img_field{onclick: "$('#file').click()"} %label.image_input_btn テキスト = f.file_field :image, autofocus: true, class: 'content-image', id: 'file' = f.hidden_field :image_cache, class: 'hidden' .pharse = f.label :pharse, "テキスト", class: 'input-content', size: 25 %span.form-require テキスト %br/ = f.text_field :pharse, autofocus: true, placeholder: テキスト", class: 'content-default' .summery = f.label :summery, "テキスト", class: 'input-content' %span.form-require テキスト %br/ = f.text_area :summery, autofocus: true, placeholder: "テキスト", class: 'content-summery' .post-btn = f.submit '投稿する', {class: 'btn-red'} =render partial: "footer"
[post.rb] class Post < ApplicationRecord has_many :comments has_many :likes belongs_to :user validates :title, {presence: true, length: {maximum: 25}} validates :image, :category_id, presence: true validates :pharse, {presence: true, length: {maximum: 25}} validates :summery, {presence: true, length: {maximum: 100}} mount_uploader :image, ImageUploader end
[schema.rb] ActiveRecord::Schema.define(version: 2020_04_10_005746) do create_table "posts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "title", null: false t.string "pharse", null: false t.text "summery", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "image", null: false t.integer "category_id", null: false t.integer "user_id" end create_table "user_informations", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.bigint "user_id" t.integer "prefecture_id", default: 0, null: false t.string "city", null: false t.integer "birth_year", null: false t.integer "birth_month", null: false t.integer "birth_day", null: false t.integer "age_id", null: false t.integer "gender_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["user_id"], name: "index_user_informations_on_user_id" end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "nickname", null: false t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end add_foreign_key "user_informations", "users" end