csdn产品小助手 2011-02-23 13:25 采纳率: 0%
浏览 347

远程ajax请求中的问题

Excuse me,I am using rails 2.3.8 . I have a remote form in a popup. this use a javascript code for submit post because i need that post in multipart. OK?

The partial of form in popup is:

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

My controller

  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

However when finished action I have this error in console of firebug:

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

so render :update don´t update my page.

NOTE: I save the product properly.

why this error occurs?

Thank in advance

  • 写回答

1条回答 默认 最新

  • weixin_33724659 2011-02-23 18:50
    关注

    in controller add

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

    then add code in jQuery according the functionality inside

          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();
                        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64