25

Using rails 4, and trying to add a file field to an existing form, using simple_form and paperclip.

Here's the critical part of the form:

<%= simple_form_for(@employee, html: { class: 'form-horizontal requires', multipart: true}, remote: true) do |f| %>

    <%= f.input :avatar %>

<% end %>

Everything works ok, unless I actually submit the form with an uploaded file. Then, I get this:

ActionController::InvalidAuthenticityToken in EmployeesController#update

What am I doing wrong here?

2
  • 2
    I believe your answer is [here][1] Looks like new with rails 4. [1]: stackoverflow.com/questions/16258911/rails-4-authenticity-token Commented Sep 3, 2013 at 15:59
  • The problem is combining multipart with remote: true. This will force the form submission as HTML rather than JS. Removing the multipart will force it as JS, but then you cannot upload a file. Obviously, this is a bug, and the remotipart gem apparently is a fix. Commented Jun 15, 2018 at 21:41

2 Answers 2

21

The simplest solution would just be to add authenticity_token: true to your form. Like this:

<%= form_for @employee, html: { class: 'form-horizontal requires'}, multipart: true, remote: true, authenticity_token: true  do |f| %>
  <%= f.input :avatar %>
<% end %>
Sign up to request clarification or add additional context in comments.

2 Comments

adding authenticity_token: true worked like a charm :-)
be warned that remote: true option will be ignored and the form is submitted as html. If you want to submit the form as js look at William answer.
20

I was with the same problem. <%= token_tag form_authenticity_token %> didn't work for me.

Install gem remotipart solved my problem. remotipart

1 Comment

This is the actual answer if you are trying to submit a file using remote: true and wanting to respond using a js.erb file. Install the remotipart gem (very quick and easy) and it works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.