AJcodez
AJcodez

Reputation: 34146

Rails remote form renders json in browser

I have a remote form for creating images. Each time the file input is changed, it submits the form.

<%= form_for Image.new, remote: true, multipart: true do |f| %>
  <!-- form omitted -->
<% end %>

The automatic submitting:

$("#new_image input[type=file]").on("change", function() {
  $("#new_image").submit();
});

The image is created and json is rendered in the images controller just fine:

def create
  @image = Image.new params[:image]
  if @image.save
    render json: @image, status: :success
  else
    render json: @image.errors, status: :unprocessable_entity
  end
end

The problem is then the user is redirected to the json output. How could that happen? The form is remote: true, so it shouldn't redirect right? I just want the data available for unobtrusive callbacks.

How do I get this to work?

Edit: application.js

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .

Upvotes: 1

Views: 288

Answers (1)

Tomdarkness
Tomdarkness

Reputation: 3820

Make sure you're including the helper javascript:

//= require jquery_ujs

This should be in one of the Javascript files that the view references (or you can put it in application.js assuming you've not modified anything that would prevent application.js being included). If that's not it, use Firebug or similar developer tools and look for javascript errors or warnings.

Upvotes: 1

Related Questions