简体   繁体   中英

Rails 4, submitting a form by ajax and uploading an image by paperclip gives error: InvalidAuthenticityToken

Using rails 4, I am submitting a form by ajax using remote: true in for_for tag. I have a field to upload image in the form, for image upload paperclip is used.

If I submit the form without uploading image everything works fine. But If I upload a image and then submit the same form I get following error:

ActionController::InvalidAuthenticityToken(ActionController::InvalidAuthenticityToken)

I tried using, "authenticity_token: true" as follows:

<%= form_for @user, :remote => true, authenticity_token: true, :html=>{:class=>"form_validation ", :multipart=>true }  do |f| %>

This above piece of code does not give me InvalidAuthenticityToken error but the form is submitted by html and not js(ajax). I want to submit form by ajax.

Is this a CSRF issue or any other issue related to paperclip? Can anyone help me out with this issue.

You cannot directly upload files by ajax, you need to use jQuery Form or jQuery-File-Upload plugins to make it possible. You need to go through their documentation. Take a look at this question

form_tag with remote: true does not make an ajax request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM