我已经困在这个问题上好几天了,我没有找到任何可以帮助我的东西。
这样做的目的是让price_scenarios索引视图呈现在模态弹出窗口中。对于表中显示的每个price_scenario,调用一个控制器方法"price“。该方法从模型中获取一些信息,并使用JSON进行格式化。这种获取价格的方式是为不再在项目中的前一个开发人员编写的,并且是必要的,因为计算价格值可能需要一段时间,因此在页面加载时会呈现一个小轮gif。
问题是: price_scenarios的“价格”方法没有提供任何价值。只有在模式弹出呈现的索引视图中调用"price“时,才会发生这种情况,我已经尝试了这个呈现price_scenarios索引的方法,它工作得很好。此外,该方法成功地应用于应用程序的许多其他视图中。我认为这肯定是格式响应的问题,因为从弹出窗口调用该方法,但是我尝试过的所有东西都没有改进结果。
product show.html.haml a按钮,将price_scenarios索引显示为模式弹出:
%h3
= model_class.human_attribute_name(:price)
= link_to PriceScenario.model_name.human(count: 2), formula_product_price_scenarios_path(@formula, @product), remote: true, data: {toggle: "modal", target: "#price_scenarios"}, class: "btn"
#price_scenarios(class="modal hide fade" role="dialog" aria-labelledby="Delta" aria-hidden="true")
指数和价格方法的price_scenarios_controller.rb定义
class PriceScenariosController < ApplicationController
before_filter :require_client
load_and_authorize_resource :formula
load_and_authorize_resource :product, :through => :formula
before_filter :get_tuple, only: :price
def index
@price_scenarios = @product.price_scenarios
respond_to :js
end
def price
@price_scenario = PriceScenario.find(params[:id])
@price = @price_scenario.shifted_price(@tuple)
@price_units = @product.price_units
respond_to do |format|
format.html { render partial: 'price'}
format.json { render json: {price: @price, units: @price_units}}
end
end
price_scenario index.js.haml 在模型中呈现索引视图:
$("#price_scenarios").html("#{escape_javascript(render partial: 'modal_price_scenarios')}");
在模型中呈现的_modal_price_scenarios.html.haml视图中,“价格”在其中被调用。
#modal_price_scenarios(class="modal" role="dialog" aria-labelledby="Delta" aria-hidden="true")
- model_class = PriceScenario
.modal-body
%table.table.table-striped
%thead
%tr
%th= model_class.model_name.human
%th= model_class.human_attribute_name(:price)
%th= model_class.human_attribute_name(:references)
%tbody
- @price_scenarios.each do |scenario|
%tr
%td= scenario.name
%td.price{:"data-product-price" => price_formula_product_price_scenario_path(@formula, @product, scenario)}
= image_tag('ajax-loader.gif')
-Price.html.haml局部的price_scenarios定价方法。
= "#{@price} #{@price_units}"
product_price.js
$(function(){
$('[data-product-price]').each(function(){
var url = $(this).data('product-price')
$.ajax({
url: url,
cache: false,
dataType: "json"
}).done(function( data ) {
var priceWithUnits = data['price'] + ' ' + data['units']
$('[data-product-price="' + url + '"]').html(priceWithUnits);
});
});
});
浏览器控制台和开发日志不会显示任何错误,但似乎没有执行正确的请求。通过HttpRequester启动手动请求会产生正确的结果。
显示模式后的浏览器控制台:
GET localhost:3000/es/formulas/84/products/69/price_scenarios [HTTP/1.1 200 OK 1590ms]
POST http://localhost:3000/mini-profiler-resources/results [HTTP/1.1 200 OK 154ms]
开发日志
Rendered price_scenarios/_modal_price_scenarios.html.haml (1537.1ms)
Rendered price_scenarios/index.js.haml (1542.6ms)
手动请求
GET http://localhost:3000/es/formulas/84/products/69/price_scenarios/20/price?_=1427711855278 -- response -- 200 OK
Content-Type: text/html; charset=utf-8 X-UA-Compatible: IE=Edge Cache-Control: must-revalidate, private, max-age=0 X-Request-Id: af912de9ef3e953387ddbd5f687aa1c2 X-Runtime: 2.647176 X-Miniprofiler-Ids: ["yxomxau7wdgouexw439j"] Content-Length: 18 Server: WEBrick/1.3.1 (Ruby/1.9.3/2014-02-24) Date: Mon, 30 Mar 2015 11:04:41 GMT Connection: Keep-Alive 275.391 EUR / MWh
发布于 2015-03-30 03:18:21
呈现JSON将内容类型设置为application/json,并可选择将JSON封装在回调中。预期响应将被解析(或评估)以用作数据结构。http://apidock.com/rails/ActionController/Base/render
但是,它实际上并不将散列转换为需要手动执行的JSON编码字符串:
format.json { render json: {price: @price, units: @price_units}.to_json }
我发现ActiveModel串行化对于构建JSON响应对象非常有用,如果您正在做任何复杂的事情,这些对象很快就会失控。
class PriceSerializer < ActiveModel::Serializer
has_many :price_units
attributes :name, :whatever
end
class PriceScenariosController < ApplicationController
#...
def price
# ...
format.json { render json: @price }
end
end
编辑另一个问题,如果您的模型在运行product_price.js
时可能没有实际添加到DOM中。
// Not guaranteed to have run before product_price.js
$("#price_scenarios").html("#{escape_javascript(render partial: 'modal_price_scenarios')}");
可能的解决方案是将分部添加到隐藏元素中,而不是作为javascript字符串或使用<template>
元素。
https://stackoverflow.com/questions/29342273
复制相似问题