In a rails 5.2.3
app, I have a model Post
, which uses active_storage to attach a file and has fields for duration
and place
. The duration
must be present.
class Post
has_one_attached :video
validates :duration, presence: true
end
Using simple_form
the fields in the form are declared as
<%= f.input :duration %>
<%= f.input :place %>
<%= f.input :video %>
The controller has the following logic for the create
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = 'Post has been saved'
redirect_to root_path
else
@title = 'New Post'
@user = current_user
render :new
end
end
private
def post_params
params.require(:post).permit(:duration, :video)
end
If the validation fails, the form shows value of place
, but I lose the file name for the video. This means the user has to choose the file again. How do I fix this?