1

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

Lenocam
  • 331
  • 2
  • 17

1 Answers1

0

I've had a similar issue. Changing the column name to something different than the table name fixed this for me. Don't for get to change it in the strong params and where you mount the uploader. If that doesn't work, you should share the code for your form.

virtual_monk
  • 120
  • 3
  • 12
  • 2
    After I posted, I found this question/answer: http://stackoverflow.com/questions/17937112/rails-4-strong-parameters-param-not-found-error-with-carrierwave?rq=1 I'm not sure it's exactly what I am looking for but it worked for me: I ended up changing my params to: def image_params params.fetch(:image, {}).permit(:image, :remove_image) end – Lenocam Feb 16 '16 at 20:32