我是RoR和AJAX,jquery等的新手。我正在尝试在视图中进行ajax调用,但它没有发生。
对应的控制器(product_search_controller,rb)是:
def index
@products = querySolr(params[:q])
@productsProxy = Array.new
if @products != nil
@products.each do |p|
@productsProxy.push(ProductProxy.new(p))
end
else
@productProxy = []
end
@taxons = @productsProxy.map(&:get_taxonomy).compact.uniq
respond_with("Search Results") do |format|
format.js
format.html
format.xml { render :xml => @productsProxy, :only => [:name, :permalink, :description, :mrp], :methods => [:designer, :taxons, :price] }
end
end
对应的视图(views / product_search / index.hrml.erb)是:
<%= render :partial => 'products', :locals => {:products => @productsProxy, :taxons => @taxons, :scope => self, :scope_type => "Search"} %>
<%= render :partial => 'shared/inf_scroll', :locals => {:url => "?&page=", :total_count => @total_count} %>
/views/product_search/_products.html.erb:
<% if products.empty? %>
<div class="not_found"><%= "No Products found for the selected query. Please try a different search." %></div>
<% elsif params.key?(:keywords) %>
<h3><%= t(:search_results, :keywords => h(params[:keywords])) %></h3>
<% end %>
<% if products.any? %>
<div class="product_rows">
<%= render :partial=> 'product_listing_feature', :locals => {:scope => scope, :scope_type => scope_type} %>
<div id="ql_product"></div>
<%taxons.each do |taxon|%>
<ul class="products" data-hook class="products">
<div class = "product_row">
<h1><%=taxon%></h1>
<% taxonProducts = Array.new %>
<% products.each do |product| %>
<%@ptaxon = product.get_taxonomy%>
<%if @ptaxon == taxon%>
<% taxonProducts.push(product) %>
<% end %>
<% end %>
<div class ="featured_product_list">
<ul class = "featured_products">
<div class = "page">
<%= render :partial=> 'product_listing', :locals=>{:collection=> taxonProducts} %>
</div>
</ul>
</div>
</div>
</ul>
<% end %>
</div>
</div>
<% end %>
即它呈现另一个部分_product_listing,其中包含以下脚本:
<script>
$("document").ready(function(){
$("li#product_<%=product.productId%>").hover(
function(){$("div#quick_look_<%=product.productId%>").css("visibility", "visible");},
function(){$("div#quick_look_<%=product.productId%>").css("visibility", "hidden");}
);
$("div#quick_look_<%=product.productId%>").css("visibility", "hidden");
});
hide_sold_out({
url: "<%= sold_out_status_taxons_url(@taxon) %>",
data: {
pids: "<%= collection.map(&:id).join(",") %>"
}
});
</script>
助手:
var hide_sold_out = function(options){
$.ajax({
url: options["url"],
data: options["data"],
type: 'get',
dataType: 'text',
success: function(data){
pids = data.split(',');
for(var i = 0; i < pids.length; i++) {
if($("li#product_"+pids[i]).children("div.info").children("span.offer.sold").length == 0) {
$("li#product_"+pids[0]).children("div.info").append("<span class='offer sold'></div>");
$("li#product_"+pids[0]).children("div.info").children("div.product_listing_info_hover").children("div.listing_buttons").html("");
}
}
},
error: function(data){
console.log(data);
}
。我尝试在/views/product_search/index.js.erb中使用以下内容:
$("ul.products").append("<div class='page'><%= escape_javascript(render(:partial => 'product_listing', :locals=>{:collection=> @productsProxy})) %></div></div>")
这不起作用。然后我试了一下:
<%@taxons.each do |taxon|%>
<%taxonProducts = Array.new%>
<%@productsProxy.each do |product|%>
<%@ptaxon=product.get_taxonomy%>
<%if @ptaxon==taxon%>
<%taxonProducts.push(product)%>
<%end%>
<%end%>
$("ul.products").append("<div class='page'><%= escape_javascript(render(:partial => 'product_listing', :locals=>{:collection=> @taxonProducts})) %></div></div>")
<%end%>
但AJAX调用没有做它应该做的事情。请有人帮我调试一下。感谢
答案 0 :(得分:1)
这些
url: options["url"],
data: options["data"],
应该是
url: options.url,
data: options.data,