Skip to content

Instantly share code, notes, and snippets.

@speratus
Created January 20, 2020 16:16
Show Gist options
  • Save speratus/7e070ce5f8432f311353ec6fbcd351ad to your computer and use it in GitHub Desktop.
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.
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