1

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.

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
Kalyani
  • 21
  • 5

2 Answers2

0

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

Community
  • 1
  • 1
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • 1
    Hey thanks for the reply, but i already have gem 'remotipart' in my gem file. The link which you provided above has the solution to user remotipart gem, I am exactly using same way. Now the error is not coming but for is getting submitted by HTML ( Processing by UsersController#create as HTML ) and not JS (ajax) – Kalyani Mar 19 '15 at 06:47
-1

Please read links below and I am sure you will find something useful:

rails 4 authenticity token

https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/request_forgery_protection.rb?source=cc

Community
  • 1
  • 1
ROR
  • 441
  • 2
  • 13