Hassan-Zahid
Hassan-Zahid

Reputation: 427

422 Unprocessable Entity in rails

I am trying to implement upload a file functionality in my ror website. The file is uploaded by drag and drop on a div

I can access the file info using

e.originalEvent.dataTransfer.files[0].name
e.originalEvent.dataTransfer.files[0].size

and for uploading the file

upload(e.originalEvent.dataTransfer.files[0]);

function upload(myfile) {
        var fd = new FormData();
        fd.append("name", myfile.name);
        fd.append("fileToUpload", myfile);
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "upload_main_file");
        xhr.send(fd);
}

controller code is

  def upload_main_file
    render :text => params[:name]
  end

Route is

post 'upload_material/upload_main_file'

but in response I get the 422 Unprocessable Entity error

What is the problem

Upvotes: 1

Views: 2374

Answers (1)

Hassan-Zahid
Hassan-Zahid

Reputation: 427

Adding this line at the start of upload_main_file function fixed the problem

skip_before_action :verify_authenticity_token

Upvotes: 1

Related Questions