発生している問題・エラーメッセージ
現在CarrirWaveを用いて画像をアップロードする機能を作っているのですが、
どうしても画像がDBに格納されません。。
是非とも皆様の知恵をお借りできれば幸いです。
パラメーターには画像名が入っているのですが、@projectには格納されていない状態なためDBには保存されておりません。
Started POST "/admin/project/complete" for ::1 at 2021-01-29 03:55:34 +0900
Processing by ProjectsController#complete as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PW/4od6USTkRYA/WejU0IdrzwYUOwnJa4D8B8X7Ucq0EouC3d3FtWIzw4cpRl+fMYnPcROrHs8zyEykNIwi7rg==", "project"=>{"page_title"=>"test", "page_description"=>"test", "category_id"=>"1", "image"=>"/uploads/tmp/1611860130-792812304018408-0001-1745/4F24F1AA-6D96-4ECA-93B2-517767B400CE.JPG", "contents"=>"<p>test</p>"}, "commit"=>"内容を確認する >"}
From: /test-app/app/controllers/projects_controller.rb:24 ProjectsController#complete:
22: def complete
23: @project = Project.new(project_params)
=> 24: binding.pry
25: if @project.save!
26: redirect_to :action => 'admin_index'
27: else
28: render :action => 'new'
29: end
30: end
[1] pry(#<ProjectsController>)> @project
=> #<Project:0x00007fdee0a3e640
id: nil,
page_title: "test",
page_description: "test",
category_id: 1,
image: nil,
contents: "<p>test</p>",
author_user_id: nil,
last_update_user_id: nil,
created_at: nil,
updated_at: nil>
なお、public/uploads/tmpへは保存されております。
そのまま処理を進めると、やはりDBに格納する直前でバリデーションがかかります。
Started POST "/admin/project/complete" for ::1 at 2021-01-29 04:52:18 +0900
Processing by ProjectsController#complete as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Bl4b0y0RxyelZ4+fe+rF81IuJxjgB0uw3NROpcyJEnM/kwPFhPTjRjj3YYNQSBYe6q462QQCiibO+GZZkVXbcA==", "project"=>{"page_title"=>"wet", "page_description"=>"wet", "category_id"=>"1", "image"=>"/uploads/tmp/1611863536-108188832449489-0007-9172/4F24F1AA-6D96-4ECA-93B2-517767B400CE.JPG", "contents"=>"<p>qwet</p>"}, "commit"=>"内容を確認する >"}
(0.1ms) BEGIN
↳ app/controllers/projects_controller.rb:24
(0.1ms) ROLLBACK
↳ app/controllers/projects_controller.rb:24
Completed 422 Unprocessable Entity in 3ms (ActiveRecord: 0.2ms)
ActiveRecord::RecordInvalid (バリデーションに失敗しました: Imageを入力してください):
app/controllers/projects_controller.rb:24:in `complete'
該当のソースコード
view/project/new.html.haml
- provide(:page_title, 'PROJECT 新規作成')
.adminCommonContainer
= simple_form_for @project, url: admin_project_confirm_path, html: {multipart: true} do |f|
.inputField
= f.input :page_title, label: "タイトル", error: false
.inputField
= f.input :page_description, label: "ディスクリプション", error: false
.inputField
= f.input :category_id, as: :select, collection: [ ['Workshop', 1], ['Conference', 2] ], label: "カテゴリー", error: false
.inputField
= f.file_field :image, label: "サムネ写真", error: false
= f.hidden_field :image_cache
.inputField
= f.text_area :contents, label: "コンテンツ", error: false, class: "project_contents"
.submitButton
= f.submit '内容を確認する >', class: 'link__box'
controller/project_controller.rb
class ProjectsController < ApplicationController
def admin_index
@projects = Project.all.order('created_at DESC')
render :layout => 'admin_application'
end
def new
@project = Project.new
render :layout => 'admin_application'
end
def confirm
@project = Project.new(project_params)
render :layout => 'admin_application'
end
def complete
@project = Project.new(project_params)
binding.pry
if @project.save!
redirect_to :action => 'admin_index'
else
render :action => 'new'
end
end
private
def project_params
params.require(:project).permit(:page_title, :page_description, :category_id, :image, :image_cache, :contents, :author_user_id, :last_update_user_id)
end
end
model/project.rb
class Project < ApplicationRecord
mount_uploader :image, ImageUploader
validates :page_title, presence: true
validates :page_description, presence: true
validates :category_id, presence: true
validates :image, presence: true
validates :contents, presence: true
end
uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
# コメントアウト部省略
storage :file
# コメントアウト部省略
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# コメントアウト部省略
end
db/schema.rb
ActiveRecord::Schema.define(version: 2021_01_28_165336) do
create_table "projects", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "page_title"
t.text "page_description"
t.integer "category_id"
t.string "image"
t.text "contents"
t.integer "author_user_id"
t.integer "last_update_user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
補足情報(FW/ツールのバージョンなど)
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin19]
rails 5.2.4.4
mysql Ver 14.14 Distrib 5.7.32, for osx10.15 (x86_64) using EditLine wrapper
carrierwave (2.1.0)