Search code examples
ruby-on-railsajaxruby-on-rails-4carrierwavesimple-form

Rails: Process form with CarrierWave as JS and not HTML


I have a simple form that also adds a file to CarrierWave.

<%= simple_form_for @person, url: { controller: 'posts', action: 'create'}, remote: true do |f| %>
  <%= f.input :name %>
  <%= f.simple_fields_for :image_name do |i| %>
    <%= i.input :file %>
  <% end %>
  <div class="form-actions">
     <%= f.submit "Add New Person" %>
  </div>
<% end %>

The images are mounted in PersonImages < ActiveRecord::Base

In PersonController I have

def new
  @person = Person.new
  @person.build_person_image
end

and..

def create
  @person = Person.new(person_params)

  if @ship.save
    @persons = Person.all
    render :person_list and return
  end
end

If I only enter a name for the person name field and submit it works. Note the remote request. Console: Processing by PersonController#create as JS

The person list partial is rendered.

But if I also add an image file it fails. Note the rest is no longer a remote one.

Console: `Processing by PersonController#create as HTML`

The image is uploaded, processed and persisted successfully however Rails now wants to render a persons/create html template and not the persons/create.js.erb template.

Any ideas?


Solution

  • Sorry for only responding now. I did try all the above suggested solutions with no luck. Turns out requests where being made twice. The solution was to swap out the jquery_ujs for rails-ujs aand all is well now.

    source: https://stackoverflow.com/a/44057260/5268472