Rails 4 remotipart ajax图片上传无法正常工作

时间:2014-06-29 23:39:26

标签: jquery ruby-on-rails ajax upload

老实说,无论如何我都找不到错误,仍在尝试,但已经持续数小时。 ajax工作,当我提交表单时,页面会使用新数据更新但没有图像

任何人都可以找到问题,这让我感到疯狂

我的js:

 $('#myform').submit(function() {  

 $('#statusbutton').attr('disabled','disabled');


if ($(this).find('textarea').val() == "")
  {
    $('#validate').show('blind').delay(5000).hide('blind');
     $(this).find('textarea').focus();
    $('#statusbutton').removeAttr('disabled');
    return ;
  }


    var valuesToSubmit = $(this).serialize();  
      $(this).find('textarea').addClass('uneditable-input').attr('disabled', 'disabled');
    $.ajax({
        url: $(this).attr('action'), //sumbits it to the given url of the form
        data: valuesToSubmit,
        type: "POST",
      dataType: "text",
     // Don't process the files    
      beforeSend:function()
       {
         $('#statusbutton').html('<i class="fa fa-spinner fa-spin"></i>');

         $('#jellylogo').html('<div style="color: #3399ff; margin-left: 5px; margin-top: -1px;"> <i class="fa fa-refresh fa-spin fa-lg"></i></div>');
       },
      success: function(data){






              $('#indexstream').prepend(data).show('blind');


        }

    }).complete(function(event, data, status, xhr){


        //act on result.
      //alert("submitted");
      $('#jellylogo').html('<div id="square"></div>');

$('#statusbutton').removeAttr('disabled');
         $('#statusbutton').html('<i class="fa fa-check"></i>');
     $("#disabletext").removeClass('uneditable-input').removeAttr('disabled', 'disabled').val('');



      $("#userlink").hide().show("slow").effect('highlight', {color: 'rgb(255, 251, 204);'}, 3000);







    $(function() {
  $("abbr.timeago").timeago();
});




    });


    return false; // prevents normal behaviour




});

形式:

<%= simple_form_for Post.new, html: {:'data-type' => :text, :multipart => true}, :html => {:id => "myform"} , :remote => true  do |f| %>


   <%= hidden_field_tag :user_id, current_user.id %>

      <%= f.hidden_field :price, :as => :hidden, :value => 0 %>
      <%= f.hidden_field :description, :as => :hidden, :value => "status" %>



       <div class="field" style="margin-top: -10px; color: #4AC948;">

         <h2 style="font-weight: bold; font-size: 130%;">status post(blub it out!):</h2>

         <%= f.text_area :title, :class => "fruute-input", :id => "disabletext", :size => 29, :autofocus => true, :style => "height: 60px; line-height: 1; width: 230px; " %>
  </div>
        <br />



       <div id="validate" style="color: red;">
          <i>field can't be blank</i>
        </div>



        <!-- f.file_field :asset1 -->

        <%= f.input :asset1, as: :file, input_html: { hidden: true, width: "20px" }, label: '<i class="fa fa-camera-retro fa-2x"></i>'.html_safe %>



       <div class="actions">
         <%= button_tag(type: 'submit', class: "new-button", id: "statusbutton" )   do %>
  <i class="fa fa-check"></i>
         <% end %>

          </div>

      <% end %>


      </div>



       </div>
        </div>  
      <% end %>

发布控制器:

def create

    @post = Post.new(post_params) 
    @post.email = current_user.username
    @post.user_id = current_user.id
    @post.firstname = current_user.firstname
    @post.lastname = current_user.lastname
    @post.avatar = current_user.avatar
    @post.school = current_user.school




   respond_to do |format|
    @search = Post.search do #Post.search do
      fulltext params[:search]
      with(:school, current_user.school)
      order_by(:updated_at, :desc)
    end


    if @post.save  
      @posts = @search.results


      #format.js { render @post}
     format.js { render :js => @post, :status => :created, :location => @post }

      format.html { redirect_to posts_path, :notice => '<i class="fa fa-check fa-5x"></i>'.html_safe }
      format.json {head :ok }
       #format.json { render action: 'index', status: :created, location: @post }

      puts "----------#{@posts.count}-------------"

      # format.js {"$('#indexstream').replaceWith('<%= escape_javascript(render @post) %>');".html_safe}
      puts "-------------#{request.format}----------------"

    else
      format.html { render action: 'new' }
      format.json { render json: @post.errors, status: :unprocessable_entity }
      #format.js {alert("couldn't create post")}
    end
  end

  end

0 个答案:

没有答案