I'm trying to figure out uploading pictures, followed a tutorial on how to do so, and now I am augmenting it for my future needs.
My issue, that I am trying to create a model where the only thing you do is upload a picture, but if you hit save and the picture isn't present, then you stay where you are and are given an error warning.
The error message I am getting is this:
ActionController::ParameterMissing in ImagesController#create
param is missing or the value is empty: image
Extracted source (around line #72):
70 71 72 73 74
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:image, :remove_image)
end
end
So I acknowledge that I did a somewhat confusing thing here, naming one of the columns the same thing as my model, so here is my code:
image.rb i.e. Image Model:
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
validates_presence_of :image
validate :image_size_validation
validates :image, presence: true
private
def image_size_validation
errors[:image] << "should be less than 500KB" if image.size > 0.5.megabytes
end
end
Image Controller, just the create method:
def create
@image = Image.new(image_params)
respond_to do |format|
if @image.save
format.html { redirect_to @image, notice: 'Image was successfully created.' }
format.json { render :show, status: :created, location: @image }
else
format.html { render :new }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
I assumed the validates :image, presence: true
would fix my issue, but here I am asking for help. This is my first time trying to upload pictures/files, so whatever help you are able to give me is appreciated.
Thanks