Empty file_field on submit: parameter is missing or value is empty
I have an app with dragonfly to manage my image uploads:
- ruby 2.1.3
- rails 4.1.6
The download is in progress, but on the update action when the file is empty , I have the following message:
ActionController :: ParameterMissing in UsersController # update_avatar
no parameter or empty value: user
It makes sense because I am not passing any parameters to send. 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? Verification doesn't work.
UPDATE:
I pass the id parameter via hidden_field (I don't like this solution ...) and it solves the pairing problem.
But the check doesn't work yet.
= hidden_field(:user, :id)
source to share
So here is the answer to your question, I know it's too late for this answer, but maybe someone else will find it useful. I faced the same problem.
https://github.com/rails/rails/issues/17947
In short, if file_field
empty, the form will not submit any parameters for this file. You will find in the link above two answers how to solve your problem. Either with hiden_field_tag
or with params.fetch(:user, {})
.
source to share