Jean-Sébastien Noel Jean-Sébastien Noel - 5 months ago 49x
Ruby Question

Rails: ActionController::InvalidAuthenticityToken when adding a Image

I am new to Ruby-on-rails and I am currently working on a project that let a user log in to add,create update delete a Marvel character. Each characters have a name, description, origin, alliance and image.
I used Carrierwave for file upload.

I used the scaffold command and everything was working fine, until I decided to be able to create and update my characters on the same page using .js.erb files instead of having to redirect the user to 2 different pages for the create and the update.

I have the following error everytime I try to create a character with a image. everything works fine when I don't add a image:

I know that there are a few different other similar questions already asked on the forum but I can't seem to find the answer to my problem.

I am using Rails 4.2.6.
I tried to add the gem remotipart but it didn't fix my issue.

create.js.erb code:

$("#characters").append("<%= escape_javascript(render @character)%>");

create action in the controller:

def create
@character =

respond_to do |format|
format.html { redirect_to @character, notice: 'Character was successfully created.' }
format.json { render :show, status: :created, location: @character }
format.html { render :new }
format.json { render json: @character.errors, status: :unprocessable_entity }



I hope I provide enough information, thanks in advance!


Here is the code I have in the form.html.erb that let the users add a image:

<div class="field">
<%= f.label :image %>
<%= f.file_field :image %>
<% if f.object.image %>
<%= image_tag f.object.image.url %>
<!--<%= f.label :remove_image %>
<%= f.check_box :remove_image %> -->
<% end %>

  skip_before_action :verify_authenticity_token

Put this in your controller