ROR应用程序 - 在js.coffee中无法访问控制器中的gon变量集

时间:2014-06-11 02:24:05

标签: javascript ruby-on-rails

将详细列出实际代码所遵循的问题。特别是在一些javscript中,我无法识别我设置的Gon变量 我有一些javascript来监控用户是否更改了p值。如果是这样的话,我们会做一些js.coffee处理,然后调用控制器,然后再调用一些js.coffee。

我遇到问题的变量(相信我已设置但错误屏幕另有说明)是gon.actual_oil_p50。

整体处理是js.coffee - >控制器 - > (更多)js.coffee

drills.js.coffee(get_changed_p_values) - > drills.controller(p_value_selected) - > drills.js.coffee(displayAssumptions)

在控制器中我设置了变量gon.actual_oil_p50,然后我无法在js.coffee中访问displayAssumptions

(coffee.js)get_changes-p_values - 在p值改变时调用

get_changed_p_values = (drill_id)-> 
data = $('#x_evaluation_assumption_params').serialize()
$.ajax
   url:"/drills/#{drill_id}/p_value_selected.json",
   type:"post"
   dataType: 'json'   # data type of response
   data: data
   success: (data,success,xhr)->
     displayAssumptions data
     calculateResult drill_id
   failure: (data,success,xhr)->
     ...

(drills_controller)

  def p_value_selected
    index = params[:name][:id].to_i
    # if current discovery target is a user valuation exit - p10 etc not applicable
    if @selections[index]["user_valuation"]
      render :nothing => true
    else
      p_value =  params[:pvalue].to_s
      load_stake    
      @assum = EvaluationAssumption.new
      @assum.probability_of_success = @stake.company_specified_drill_risk

      # PMLC - set gon variable which I can't seem to access
      # in following js.coffee
      gon.actual_oil_p50 = @selections[index]["oil_p50"]
      # PMLC

      case p_value
        when "p90"
          @assum.resource_estimation_oil = @selections[index]["oil_p90"]
          @assum.resource_estimation_gas = @selections[index]["gas_p90"]
        when "p50"
         ....
      end

      respond_to do |format|
        format.json { render :json => @assum }
      end
    end
  end

(coffee.js)displayAssumptions

# (02) display the selected assumption params
displayAssumptions = (data)-> 
    $('#evaluation_assumption_resource_estimation_oil').val(data.resource_estimation_oil)
    $('#evaluation_assumption_in_ground_value_oil').val(data.in_ground_value_oil)
    $('#evaluation_assumption_resource_estimation_gas').val(data.resource_estimation_gas)
    $('#evaluation_assumption_in_ground_value_gas').val(data.in_ground_value_gas)
        $('#evaluation_assumption_probability_of_success').val(data.probability_of_success)

    # PMLC
    # now cater for when p-value selected was not p10, p50, p90, or fill to spill
    pvalue_slider = document.getElementById("pvalue").value

    if pvalue_slider.indexOf("%") > -1 
      # PMLC - this value should be set from controller
      #     but error message show otherwise
      gon.actual_oil_p50 = gon.watch.actual_oil_p50

      percChanceOil = parseInt(pvalue_slider.split "%", 1) / 100
      amt_oil = jStat.lognormal.inv((1-percChanceOil),gon.mean,gon.stdDev)
      # then shift right or left depending on mean value p50) for this oil00
      amt_oil = amt_oil * (( gon.actual_oil_p50 / gon.std_oil_p50) )

      $('#evaluation_assumption_resource_estimation_oil').val(amt_oil)

在view / layouts / application.html.erb中注意我有

<head>    
    <title><%= content_for?(:title) ? yield(:title) : "Drill Investor" %></title>
    <%= include_gon(watch:true) %> 
    <%= stylesheet_link_tag    "application", :media => "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
</head> 

结果错误

未捕获的TypeError:无法读取未定义

的属性'actual_oil_p50'

查看我得到的页面源代码 window.gon = {}; gon.mean = 6.21664219934878; gon.stdDev = 0.499350777509958; gon.std_oil_p50 = 500;
即gon.actual_oil_p50未设置

然而,在控制器中,我在设置gon.actual_p50_value后放置了消息,例如在控制器中的语句之后

gon.actual_oil_p50 = @selections[index]["oil_p50"]
puts "did I set gon.actual_oil_p50+ + gon.actual_oil_p50

它告诉我它已被适当地设置

非常感谢提前fpr任何提示/答案 - 皮埃尔

1 个答案:

答案 0 :(得分:1)

要么渲染JSON,要么没有GON定义,或者没有。

我不确定该设置会如何传播回HTML页面。