Created
January 20, 2020 16:16
-
-
Save speratus/7e070ce5f8432f311353ec6fbcd351ad to your computer and use it in GitHub Desktop.
A controller to handle an uploaded file and pass it to ActiveStorage correctly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create | |
#If you set up your frontend request properly, params[:avatar] will be an UploadedFile instance, which is what we want | |
user = User.new(name: params[:name], avatar: params[:avatar]) | |
#passing the file in to the constructor causes ActiveStorage to attach the file automatically. | |
if user.save | |
puts "The user saved successfully" | |
render json: user | |
else | |
puts "The user failed to save" | |
render json: {message: "Error"} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment