mercredi 12 août 2015

form_for does not pass the required value when user submits blank form

I have the following form:

  # controller
  def edit
    @doc = HomeworkDocument.find(params[:id])
  end

  # view
  <%= form_for @doc, 
      url: student_homework_document_path(student_id: @doc.submitter_id,
                                           id: @doc.id),
      html: { multipart: true } do |f| %>

  <%= f.label :file1 %>
  <%= f.file_field :file1 %>

  <%= f.label :file2 %>
  <%= f.file_field :file2 %>

  <%= f.submit "Submit" %>

When the user submits either one or both of file1 and file2, the form works fine. However, when the user clicks submit without any file, exception is raised:

param is missing or the value is empty: homework_document

due to my strong param specification:

def doc_grader_params
  params.require(:homework_document)
        .permit(:file1, :file2)
end

The param that gets passed is (notice no homework_document is passed):

Parameters:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"QJirOW/HQ3sv8AR/yArW6cQ2bmvz0j5D8G6czu45lLA=",
 "commit"=>"Submit grading",
 "student_id"=>"4",
 "id"=>"21"}

Why does this happen and how to to include an (empty) homework_document hash in param even when the user submits blank form?




Aucun commentaire:

Enregistrer un commentaire