422 Unprocessable Entity in rails

点点圈 提交于 2020-01-23 19:02:05

问题


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


回答1:


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

skip_before_action :verify_authenticity_token


来源:https://stackoverflow.com/questions/27395854/422-unprocessable-entity-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!