简体   繁体   English

带有remotipart的载波AJAX上传

[英]carrierwave AJAX upload with remotipart

In my rails4 I would like to submit a form via AJAX that has a file uploaded via carrierwave . 在我的rails4中,我想通过AJAX提交一个表单,该表单具有一个通过carrierwave上传的文件。 As far as I know it can be done via remotipart , but can't figure it out how I can do that. 据我所知,可以通过remotipart完成,但无法弄清楚我该怎么做。 (Remotipart installed properly since it's working with refile gem that I also use in the app.) (Remotipart已正确安装,因为它可以与我也在应用程序中使用的重新归档gem一起使用。)

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. 在当前代码下方的情况下,将根据日志提交表单,并说已渲染create.js.erb ,但create.js.erb文件中的代码永远不会运行。

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 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 _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. UPDATE:问题与更新post对象有关。 The update form code can be found in the _post.html.erb , so that's why <%= j render @post %> didn't work. 可以在_post.html.erb找到更新表单代码,这就是为什么<%= j render @post %>不起作用的原因。 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 所以有效的方法:1.创建没有图像的帖子2.更新没有图像的帖子3.更新具有图像但没有图像的帖子不起作用:1.创建具有图像的帖子2.更新具有图像的帖子被编辑

<%= 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 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 %>

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. 我更改了模态的结构,现在可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM