如果不在Rails中刷新浏览器,AJAX请求无法看到效果 [英] The AJAX request cannot see the effect without refresh the browser in Rails

查看:99
本文介绍了如果不在Rails中刷新浏览器,AJAX请求无法看到效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与这个人面对同样的问题



我将rjs更改为js.erb,就像他一样。我们都使用<%= button_to'Add to Cart',line_items_path(:product_id => product),:remote => true%> 来发送向控制器发送AJAX请求。 format.js 来罚款并执行create.js.erb。但购物车没有添加任何东西。



记录结果:

渲染的line_items / (4.3ms)
渲染的购物车/_cart.html.erb(8.0ms)
呈现的line_items / create.js.erb(8.8ms)



这是我们发送AJAX请求的index.html.erb

 < ;%如果通知%> 
< p id =notice><%= notice%>< / p>
<%end%>
< h1>您的实用目录< / h1>

<%@ products.each do | product | %GT;
< div class =entry>
<%= link_to image_tag(product.image_url),line_items_path(:product_id => product),html_options = {:method => :post}%>
< h3><%= product.title%>< / h3>
<%=清理product.description%>
< div class =price_line>
< span class =price><%= number_to_currency(product.price,:precision => 3)%>< / span>
<%= button_to'加入购物车',line_items_path(:product_id => product),:remote => true%>
< / div>
< / div>
<%end%>

这是处理请求的line_items控制器函数

 #POST / line_items 
#POST /line_items.json
def创建
#仅用于练习
session [:counter] =无

@cart = current_cart
product = Product.find(params [:product_id])
@line_item = @ cart.add_product(product.id)



respond_to | format |
if @ line_item.save
format.html {redirect_to store_index_path}
format.js
format.json {render json:@line_item,status :::created,location:@line_item }
else
format.html {render action:new}
format.json {render json:@ line_item.errors,status::unprocessable_entity}
end
结束


结束

create.js.erb

  $('#cart')。html(<%= escape_javascript(render(@cart))%> ); 


解决方案

我解决了这个问题。
感谢伟大的文章,它告诉我firebug能够从AJAX请求中查看响应的来源。
并且 JSLint 可帮助我检出JavaScript语法。
最后,我要感谢Firebug,它是一个非常棒的工具。



问题是,如果有JavaScript是任何语法错误。

在我的问题中:



我应该使用单引号而不是双引号来包装渲染结果。渲染结果与许多带有和的HTML一起出现,它们包装它们会导致javascript语法错误。 (不允许双重双击)



因此,我只需更改 $('#cart')。html(< %= escape_javascript(render(@cart))%>);
$('#cart')。html('<%= escape_javascript (render(@cart))%>');



我希望这个答案能够帮助其他遭受苦难的人这个噩梦的工作人员。
如果可能,请帮助我提高问题率:)

I faced the same problem with this guy

I change rjs to js.erb just like him. And we all use <%= button_to 'Add to Cart',line_items_path(:product_id => product) ,:remote=>true %> to send an AJAX request to the controller. format.js to fine and execute create.js.erb. But the cart did not add anything.

log result :

Rendered line_items/_line_item.html.erb (4.3ms) Rendered carts/_cart.html.erb (8.0ms) Rendered line_items/create.js.erb (8.8ms)

That's the index.html.erb we send the AJAX request

<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>

<% @products.each do |product| %>
<div class="entry">
<%= link_to image_tag(product.image_url), line_items_path(:product_id => product), html_options = {:method => :post} %>
    <h3><%= product.title %></h3>
    <%=sanitize product.description %>
    <div class="price_line">
      <span class="price"><%= number_to_currency(product.price,:precision=>3) %></span>
      <%= button_to 'Add to Cart',line_items_path(:product_id => product) ,:remote=>true %>
    </div>
  </div>
<% end %>

That's the line_items controller function to handle the request

  # POST /line_items
  # POST /line_items.json
  def create
     # for exercise only
    session[:counter] = nil

    @cart = current_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product.id)



    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_index_path }
        format.js
        format.json { render json: @line_item, status: :created, location: @line_item }
      else
        format.html { render action: "new" }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end


  end

create.js.erb

$('#cart').html("<%= escape_javascript(render(@cart)) %>");

解决方案

I fixed the problem. Thanks to the great article which tell me the ability of firebug to see the source of response from AJAX request. And JSLint helps me checkout the javascript syntax. And finally I would like to thanks the Firebug which is such a great tool.

The problem is that the javascript is not being executed if there is any syntax error.

In my problem:

I should use single-quoted instead of double-qouted to wrap the render results. The render results comes out with many HTML with "", and "" which wrap them will cause syntax error in javascript. (double-qouted in double-qouated is not allowed)

So I simply change $('#cart').html("<%= escape_javascript(render(@cart)) %>"); to $('#cart').html('<%= escape_javascript(render(@cart))%>');

I hope this answer will help the others who also suffer from this nightmare staff. Help me increase the question rate if this is possible :)

这篇关于如果不在Rails中刷新浏览器,AJAX请求无法看到效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆