2011-02-23 24 views
0

실례합니다. 레일을 사용하고 있습니다. 2.3.8. 팝업에 원격 양식이 있습니다. 내가 multipart에 그 게시물이 필요하기 때문에 이것은 제출 게시물에 대한 자바 스크립트 코드를 사용합니다. 승인? 원격 Ajax 요청의 문제

팝업의 형태의 부분

은 다음과 같습니다

<script type="text/javascript"> 
    $j(document).ready(function() { 
     $j('#product_submit').click(function(event) { 
      event.preventDefault(); 
      $j('#uploadForm').ajaxSubmit({ 
       beforeSubmit: function(a, f, o) { 
        o.dataType = 'json'; 
       }, 
       complete: function(XMLHttpRequest, textStatus) { 
        // XMLHttpRequest.responseText will contain the URL of the uploaded image. 
        // Put it in an image element you create, or do with it what you will. 
        // For example, if you have an image elemtn with id "my_image", then 
        // $('#my_image').attr('src', XMLHttpRequest.responseText); 
        // Will set that image tag to display the uploaded image. 
       } 
      }); 
     }); 
    }); 
</script> 
<div id="new-starred-product" class="popup clearfix"> 
    <div class="header">Nuevo producto estrella</div> 
    <div id="messages"> 

    </div> 
    <% remote_form_for @product, :url => {:controller => "products", :action => "create_starred_product"}, :html => {:method => :post, :id => 'uploadForm', :multipart => true} do |f| %> 
     <div class="content"> 
     <label>Nombre:</label> <%= f.text_field :name, :class => "textarea" %> 
     <br/> 
     <label>Precio:</label> <%= f.text_field :price, :class => "textarea" %> 
     <br/> 
     <%#= f.file_field :photo %> 
     <div class="images-selector"> 

      <% count = 0 %> 
      <%# f.fields_for "product_images_attributes[]", ProductImage.new do |builder| %> 
      <% f.fields_for :product_images, 4.times { ProductImage.new } do |builder| %> 
       <% if builder.object.new_record? %> 
        <label> 
        Imagen <%= "#{count = count + 1}" %> 
        </label> 
        <%= builder.file_field :photo, :class => "file-upload" -%> 
        <br/> 
        <br/> 
       <% end %> 
       <%#= builder.text_field :testfield %> 
       <!--ACA VA EL CODIGO PARA IMAGENES--> 
      <% end %> 
     </div> 
     <br/> 
     <label>Descripción</label><%= f.text_area :description, :rows => 10, :class => "textarea clearfix" -%> 
     <br/> 

     <p> 
      <%#= f.submit "Crear" %> 
      <%= link_to "Crear", "#", :id => "product_submit" %> 
     </p> 
     </div> 
    <% end %> 
</div> 

내 컨트롤러

def create_starred_product 
    product = Product.new(params[:product]) 
    if product.valid? && product.save 
     render :update do |page| 
     #puts in the page the name of product 
     page.replace_html 'star-product', :partial => "admin/products/star_product_name", :locals => {:star_product => product} 
     #Close the popup. I use $j for conflict between JQuery and Prototype 
     page.call "$j.modal.close" 
     end 
    else 
    end 
    end 

조치를 완료 때 나는 불을 지르고의 콘솔에서이 오류가 있습니다

s.error.call을 (s.context, xhr, 'error', e);

그래서 렌더링 : 업데이트 내 페이지를 업데이트하지 마세요.

참고 : 제품을 올바르게 저장합니다.

왜이 오류가 발생합니까? 컨트롤러

답변

0

respond_to do |format| 
    format.js { render :json => product } 
end 

다음

 complete: function(XMLHttpRequest, textStatus) { 
         // XMLHttpRequest.responseText will contain the URL of the uploaded image. 
         // Put it in an image element you create, or do with it what you will. 
         // For example, if you have an image elemtn with id "my_image", then 
         // $('#my_image').attr('src', XMLHttpRequest.responseText); 
         // Will set that image tag to display the uploaded image. 
         //HERE ADD YOUR CODE FOR UPDATE PAGE EXAMPLE 
         //parseJson my data 
         var product = $j.parseJSON(XMLHttpRequest.responseText); 
         //add info in page in rails is render :update 
        $j("#star-product").html("<input type='hidden' id='star-product-id' value='" + product.product.id.toString() + "' name='star-product'/>"); 
        $j("#star-product").append("<span id='star-product-name-span'>"+ product.product.name.toString() + "</span>"); 
        //close the modal 
        $j.modal.close(); 
        } 
내부 기능에있어서 jQuery를 코드 추가 미리

감사

관련 문제