Multi step form with image uploader
Actually I solved my problem. Here's working code for multistep forms with file uploading using carrierwave
if params[:user][:img_path]
@uploaded = params[:user][:img_path]
params[:user].delete(:img_path)
end
session[:user_data].deep_merge!(params[:user]) if params[:user]
@user = User.new(session[:user_data])
if @uploaded
# here how validation will work
@user.img_path = @uploaded
end
@user.current_stage = session[:register_stage]
if @user.valid?
if @user.last_stage?
@user.img_path = session[:img] if @user.last_stage?
@user.save
else
@user.next_stage
end
# now we can store carrierwave object in session
session[:img] = @user.img_path
session[:register_stage] = @user.current_stage
end
Save multiple image in react with a multiple step form
You seem to be treating formData
as a plain object but for posting multipart/form-data
, you should be using a FormData
instance.
First, create formData
as a ref
so it's not recreated for every render. You can also create a function to pass to your components for adding files
// in ProductForm
const formData = useRef(new FormData());
const addFoto = (file) => {
formData.current.append("foto", file);
};
Now you can pass the addFoto
function to your components<Basics addFoto={addFoto} />
and use it in your Media
componentexport default function Media({ addFoto }) {
const handleChange = (file) => {
addFoto(file);
};
// ...
Finally, back in your ProductForm
component, post the data like this// no custom headers required
axios.post(`${URL}/api/productos`, formData.current);
Where can I temporarily store file uploads in a React multi step form?
Add state to the parent component that houses all the form steps and then store all of the form data in local state. Once you are ready to submit, use the state values to submit.
You can store a file in react state just like you would any other form input. The file will be a variable obtained from event.target.files
Related Topics
Why The Unit Test Frameworks in Fortran Rely on Ruby Instead of Fortran Itself
Gem Ransack Doesn't Return Any Results When Searched with Full Name
Persistent Tcp Connection in Rails App
Sorting a Multidimensional Array in Ruby
Rails - Understanding Application.Js and Application.CSS
Difference Between @@ and @ in Ruby
Rails Validating Search Params
What's The Best Way to Test Delayed_Job Chains with Rspec
Can't Install Debugger Gem - Rails - MAC Osx Mavericks
Accessing Microsoft Exchange Server from Ruby
Rails on Netbeans: Uncaught Exception: No Such File to Load - Script/Server or Script/Console
How to Print a Multi-Dimensional Array in Ruby
Ruby 2.0 Bytecode Export/Import
Remove Adjacent Identical Elements in a Ruby Array
"Previous Post" and "Next Post" Link in Show View (Nested Resources)
Why Does Including This Module Not Override a Dynamically-Generated Method