Currently, I am creating a function to upload images using CarrirWave.
The image will not be stored in DB.
I hope we can use your wisdom.
The parameter contains the image name, but it is not stored in DB because it is not stored in @project.
Started POST "/admin/project/complete" for::1 at 2021-01-2903: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" =>"Check Content >"}
From:/test-app/app/controllers/projects_controller.rb:24ProjectsController#complete:
22—def complete
23:@project=Project.new(project_params)
=>24: binding.ply
25: [email protected]!
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>
Also, it is stored in public/uploads/tmp.
If you proceed as it is, it will still be validated just before storing it in DB.
Started POST "/admin/project/complete" for::1 at 2021-01-2904: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" =>"Check Content >"}
(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: Enter Image:
app/controllers/projects_controller.rb:24:in `complete'
view/project/new.html.haml
-provide(:page_title, 'PROJECT New')
.adminCommonContainer
= simple_form_for@project, url:admin_project_confirm_path, html:{multipart:true} do | f |
.inputField
= f.input:page_title, label: "Title", error: false
.inputField
= f.input:page_description, label:"description", error:false
.inputField
= f.input:category_id,as::select,collection:['Workshop',1],['Conference',2],label:"category",error:false
.inputField
= f. file_field: image, label: "Thumbnail Photo", error: false
= f.hidden_field —image_cache
.inputField
= f.text_area:contents, label: "Content", error: false, class: "project_contents"
.submitButton
= f.submit 'Check Content >', class: 'link__box'
controller/project_controller.rb
class ProjectsController<ApplicationController
default 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.ply
[email protected]!
redirect_to: action=> 'admin_index'
else
render:action=>'new'
end
end
private
def project_params
param.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
values:page_title,presence:true
values:page_description,presence:true
values:category_id,presence:true
values:image,presence:true
values:contents, presence:true
end
uploaders/image_uploader.rb
class ImageUploader<CarrierWave::Uploader::Base
# comment-out omission
storage:file
# comment-out omission
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# comment-out omission
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
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, forosx 10.15 (x86_64) using EditLine wrapper
carrierwave (2.1.0)
I solved myself.
I put the confirmation screen in the form of new→confirm→complete, but I didn't set cache at that time, so it seems that there was no image when I went from confirm to complete.
I changed the view and controller as follows.
BEFORE:view/project/confirm.html.haml
# omitted
.inputField
= f. input: image, as: : hidden, label: "thumbnail image", error: false
= @project.image
# omission
AFTER:view/project/confirm.html.haml
# omitted
.inputField
= image_tag(@project.image.url)#Change here
= hidden_field_tag: "cache [image]", @project.image.cache_name #Change here
# omission
BEFORE:controller/project_controller.rb
# omitted
def complete
@project=Project.new(project_params)
[email protected]!
redirect_to: action=> 'admin_index'
else
render:action=>'new'
end
end
# omission
AFTER:controller/project_controller.rb
# omitted
def complete
@project=Project.new(project_params)
@project.image.retrieve_from_cache!params[:cache][:image]#Add this
[email protected]!
redirect_to: action=> 'admin_index'
else
render:action=>'new'
end
end
# omission
© 2024 OneMinuteCode. All rights reserved.