#####問題点
ブログ記事投稿アプリを作ってます。
ローカル環境では記事投稿しても問題なくデータベースに値(タイトル、文章、カテゴリ、画像、日時)が保存されるのですが本番環境では投稿ボタンを押しても、データベースに値が保存されずに困っています。
画像はawsのS3に保存されるようになっていてローカル環境では問題なくS3側に画像データが保存されています。
#####確認してみたこと
sshでログインしてサーバー側でproduction.log unicorn.stderr.log unicorn.stdout.logのlogを確認してみたのですがエラー文章もなくproduction.logの方での確認でcreateアクションが動いて値は受け取れてるように思います。その後rollbackの記述があったのでrollbackしているのが原因なのかと考えてます。
試しに画像ファイルなしで投稿してみたとことろ、imageがNULLでレコードとして値が保存され投稿できていました。(imageがnullによるエラーがビューで発生)
投稿ボタン押した時のログ production.log Parameters: {"utf8"=>"✓", "authenticity_token"=>"+UkbNvzLQXJxZs3o5mPpu9k7e14yhaZCwPDm5qf84 SWEPdWJhMx6PYA5PFygX5xay1/uLSWanxMSY/pXIPTVVg==", "product"=>{"name"=>"テスト", "image"=>#<ActionDispatch::Http::UploadedFile:0x0000559e5b9f9fd0 @tempfile=#<Tempfile:/tmp/RackMultip art20190601-19353-ll0jdh.jpg>, @original_filename="51DOPJPZH8L.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[image]\"; filename=\"51DOPJ PZH8L.jpg\"\r\nContent-Type: image/jpeg\r\n">, "description"=>"電車だ", "category_id"=>"4"}, "commit"=>"投稿する"}
ruby 2.5.1
rails5.2.3
本番環境AWS
画像保存先S3
ルーティング resources :products, only: [:index, :new, :show, :create, :edit,:update, :destroy]
コントローラー class ProductsController < ApplicationController before_action :set_product, only: [ :show, :edit,:update, :destroy] before_action :set_category, only: [ :index, :show, :new, :create, :edit, :update, :destroy, :category] def index @product = Product.all.order("created_at DESC").page(params[:page]).per(8) end def new if user_signed_in? @product = Product.new else redirect_to new_user_registration_path end end def create @product = Product.create(product_params) if @product.save redirect_to root_path ,notice: '記事が投稿されました' else render :new end end def show @likes = Like.find_by(user_id: current_user.id, product_id: @product.id) end def category @categories = Category.find_by(id: params[:id]) @products = Product.where(category_id: @categories.id).page(params[:page]).per(8) end def edit end def update if @product.user_id == current_user.id @product.update(product_params) redirect_to product_path(@product.id) end end def destroy if @product.user_id == current_user.id @product.destroy redirect_to root_path else redirect_to action: 'show', controller: 'products' end end private def product_params params.require(:product).permit( :name, :description, :category_id, :image).merge(user_id: current_user.id) end def set_product @product = Product.find(params[:id]) end def set_category @category = Category.all end end
ビュー = render partial: 'products/header' .new-product .new-product__box .new-product__box__main 記事投稿 = form_for @product , html: { class: "new-product__box__main__form" } do |f| %p 記事タイトル = f.text_area :name, maxlength: "20", placeholder: "タイトルを入力してください", type: "string", class: 'new-product__box__main__form__title' %p 画像 = f.file_field :image, type: "file", class: 'new-product__box__main__form__photo' %p 記事文章 = f.text_area :description, maxlength: "150", placeholder: "文章を入力してください", type: "text", class: 'new-product__box__main__form__text' %p カテゴリー選択 = f.select :category_id, @category.map{|t| [t.name, t.id]}, {}, class: 'new-product__box__main__form__category' = f.submit '投稿する', class: 'new-product__box__main__form__submit' = render partial: 'products/footer'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。