0

In my rails4 I would like to submit a form via AJAX that has a file uploaded via carrierwave. As far as I know it can be done via remotipart, but can't figure it out how I can do that. (Remotipart installed properly since it's working with refile gem that I also use in the app.)

With the current code below the form gets submitted according to the logs and it says the create.js.erb is rendered, but the code in the create.js.erb file never runs.

Rendered posts/create.js.erb (387.0ms)

What should I do?

form

<%= form_for @post, remote: true, method: :post, :html => { :multipart => true, class: "post-create-form" } do |f| %>
  <img id="img_prev" style="margin-bottom:10px;" width=300 height=200 src="#" class="img-thumbnail hidden"/>
  <%= f.text_area :body, placeholder: "Share something useful..", class: "form-control post-create-body" %>
  <%= f.button "SHARE", class: "btn btn-primary btn-sm btn-create-post", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Saving..."} %>
  <span class="btn btn-success btn-sm btn-message-file">Upload Image
    <%= f.file_field :post_image, class: "choose-message-file", id: "avatar-upload" %>
  </span>
  <%= f.hidden_field :post_image_cache %>
<% end %>

create.js.erb

$('ul.errors').hide();
$('.alert-danger').hide();
$('.post-index').prepend('<%= j render @post %>');
$('.post-create-body').val('');
$('.btn-create-post').prop('disabled',true);

_post.html.erb

    <div class="col-md-2">
      <%= link_to user_path(post.user) do %>
        <%= image_tag post.user.avatar.url(:base_thumb), class: 'post-avatar' %>
      <% end %>
    </div>

    <div class="col-md-8">
      <div class="post-info">
        <%= link_to user_path(post.user) do %>
          <span class="post-user-name"><%= post.user.full_name %></span>
        <% end %>
        <span class="post-updated"><%= local_time_ago(post.updated_at) %></span>
      </div>
      <div class="post-body">
        <%= simple_format(find_links(h post.body)) %>
      </div>
      <div class="post-image" data-postlink="<%= post_path(post) %>">
        <% if post.post_image? %>
          <button data-toggle="modal" data-target="#post-image-modal">
            <%= image_tag post.post_image.url(:base_thumb) %>
          </button>
        <% end %>
      </div>
    </div>

UPDATE: The problem is connected to updating the post object. The update form code can be found in the _post.html.erb, so that's why <%= j render @post %> didn't work. Still don't know what the problem is.

So what works: 1. creating post that has no image 2. updating post that has no image 3. updating post that has image BUT image is not edited Not working: 1. creating post that has image 2. updating post that has image which is edited

<%= form_for post, method: :patch, remote: true, :html => { :multipart => true } do |f| %>
  <div class="modal fade updatepost" id="updatepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content" style="text-align:left">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="edittaskmodalohhhhh">Edit Task</h4>
        </div>
        <div class="modal-body">
          <div class="alert alert-danger" style="display:none">
            <ul class="errors" style="display:none">
              <%= render 'layouts/error_messages', object: f.object %>
            </ul>
          </div>
          <div class="field form-group">
             <%= f.file_field :post_image, id: "avatar-upload-update" %>
             <%= f.hidden_field :post_image_cache %>
          </div>
          <div class="field form-group">
            <%= f.text_area :body, class: "form-control edit-post-body" %>
          </div>   
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal" id="updatepostclose">Close</button>
            <%= f.button "Update post", class: 'btn btn-primary edit-post-submit', data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Saving..."} %>
        </div>
      </div>
    </div>
  </div>
<% end %>

update.js.erb

$("ul.errors").html("");
<% if @post.errors.any? %>
   $('.alert-danger').show();
   $('ul.errors').show();
   <% @post.errors.full_messages.each do |message| %>
     $("ul.errors").append($("<li />").html("<%= message.html_safe %>"));
   <% end %>
<% else %>
  $('ul.errors').hide();
  $('.alert-danger').hide();
  $('#updatepost_<%= @post.id %>').modal('hide');
  $post = $('<%= j render @post %>');
  editPostHideDanger($post);
  $('#post_<%= @post.id %>').remove();
  $(".new-post-insert").prepend($post);
  $("html, body").animate({ scrollTop: 0 }, "slow");
<% end %>
Sean Magyar
  • 2,360
  • 1
  • 25
  • 57
  • Your form does not have `:html => { :multipart => true }` . Try adding this. `<%= form_for @post, remote: true, method: :post, class: "post-create-form", :html => { :multipart => true } do |f| %>` – dp7 Apr 05 '16 at 09:51
  • dkp, it didn't help :( – Sean Magyar Apr 05 '16 at 09:57
  • can you post stack trace after submitting the form ? – dp7 Apr 05 '16 at 10:00
  • I updated the question. I also checked if remotipart is used with `if remotipart_submitted?`. It's true as you see in the logs. It printed REMOTION. Form is working via js properly if there is only text. – Sean Magyar Apr 05 '16 at 10:05
  • dkp, any other idea? – Sean Magyar Apr 05 '16 at 10:54
  • `Rendered posts/_post.html.erb (78.1ms)` - This means the `post` partial is also getting rendered correctly which has been placed within `create.js.erb`. Everything seems to be correct. Just make sure your classes are available in DOM. – dp7 Apr 05 '16 at 11:05
  • I don't know what can be different, since if I share the post with no image then it's working fine. – Sean Magyar Apr 05 '16 at 11:09
  • `Unpermitted parameter: post_image_cache` - why do you have `post_image_cache` in the form when you are not permitting in controller. – dp7 Apr 05 '16 at 11:10
  • Yeah I realized 15mins ago, but with having that permitted it's still the same. I also checked with no image preview and image_cache, but it was the same. – Sean Magyar Apr 05 '16 at 11:13
  • So the problem is this line `$('.post-index').prepend('<%= j render @post %>');` in `create.js.erb`. The weird thing is that if I create the post without pic then it works fine. Any idea? – Sean Magyar Apr 05 '16 at 11:26
  • Is there something in `post` partial that is dealing with the uploaded image ? & that is silently getting skipped if you do not have an image ? – dp7 Apr 05 '16 at 11:31
  • I updated my question again. This part belongs to the displayed pic. I can't imagine what the problem can be, since if I reload the page/ share the js with no pic everything works fine. – Sean Magyar Apr 05 '16 at 11:35
  • `<% if post.post_image.present? %> <% end %>` - Can you modify this section of your partial – dp7 Apr 05 '16 at 11:41
  • Not good. I tried to take out the whole code snippet as well, but it's still the same. – Sean Magyar Apr 05 '16 at 12:07
  • dkp, getting closer. Can you guess what can go wrong with the update form? – Sean Magyar Apr 05 '16 at 13:50
  • I am not sure why `remotipart` with `remote: true` and `multipart: true` is not working for you. I have used `remotipart`, it works like a charm. I just have to install the gem and add `//= require jquery.remotipart` to my `application.js` . Your update form also looks fine to me. – dp7 Apr 05 '16 at 14:16
  • This is something connected to the bootstrap modal. I always have trouble with it. The create form and the update forms (as many as objects) are on the same page. That's the basic issue and it can causes a lot of problem. – Sean Magyar Apr 05 '16 at 14:21
  • dkp, I could solve it with changing the structure of the modals. But still don't know the reason. Bootstrap modals can behave like a bitch sometimes. And it was also hard to track down, since without the image the form worked perfectly...... – Sean Magyar Apr 05 '16 at 18:07

1 Answers1

0

There was some issue (still don't know what) with the bootstrap modals. I changed the structure of the modals and now it's working fine.

Sean Magyar
  • 2,360
  • 1
  • 25
  • 57