'Empty file_field on submit: param is missing or the value is empty

I have a application with dragonfly to manage my image uploads:

  • ruby 2.1.3
  • rails 4.1.6

The upload works but on update action, when the file is empty, I have the following message:

ActionController::ParameterMissing in UsersController#update_avatar

param is missing or the value is empty: user

It make senses because I'm passing no params on submit. But what about validation?

Model:

class User < ActiveRecord::Base
  dragonfly_accessor :avatar
  
  ...

  validates_presence_of :avatar

  ...

Controller:

class UsersController < ApplicationController
  ...

  def update_avatar
    @user = User.find_by(id: current_user.id)
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to edit_user_avatar_url, notice: t('flash.actions.update_avatar.notice') }
        format.json { render :show, status: :ok, location: @user }
       else
         format.html { render :edit }
         format.json { render json: @user.errors, status: :unprocessable_entity }
       end
     end
   end

   ...

   def user_params
     params.require(:user).permit(:username, :name, :avatar, :birth)
   end
end

VIEW:

- case params[:action]

...

- when 'edit_avatar', 'update_avatar'
  = form_for @user, url: { action: 'update_avatar'} do |f|
    ul.list-form
      li.item-form 
        = f.label :avatar
          span.label-title
            ' Foto:
          = f.file_field :avatar

    .submit-area
      = button_tag "<span class='wrapper'>Update Avatar</span>".html_safe, class: 'bt'

...

What am I doing wrong? The validation seems not working.

UPDATE:

I'm passing the id parameter via hidden_field (I don't like this solution...) and solve the parcial problem.

But the validation is still not working.

= hidden_field(:user, :id)


Solution 1:[1]

So here is the answer to your question, I know is a bit too late for this answer, but maybe some other person will find it helpful. I came across the same issue.

https://github.com/rails/rails/issues/17947

In short if file_field is empty then the form will not submit any parameters for that file. You will find on link above two answers how to solve your problem. Either with hiden_field_tag or with params.fetch(:user, {}) .

Solution 2:[2]

Try to use this code in your update action:

@user = User.find(params[:id])

Solution 3:[3]

I know this is quite old, but I had this issue myself, maybe this can help others later. Although it probably isn't the best way. It does the wonders.

The most easiest fix was adding inside of the update controller an if/else statement as represented:

def update_avatar
    if params[:user].blank?
        flash[:error] = "You must upload a file first to save new avatar."
        render :edit_profile  
    elsif current_user.update(params.require(:user).permit(:avatar))
        flash[:success] = "Avatar updated"
        redirect_to edit_user_path
    end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anyschob
Solution 2 neha
Solution 3 Mergo22