0

在 assets/javascript 中的 js.coffee 文件中,我尝试响应从对控制器的调用中收到的数据未成功。使用 js.coffee 文件中的打印语句,我可以看到控制器返回了正确的数据。在 js.coffee 文件中,我尝试了许多不同的尝试来访问此数据以放置在屏幕部分上,但均未成功。下面我还包含了 (1) js,coffee 文件 (2) 相关的控制器操作 (3) 从 javascript/drills.js.coffee 生成的终端控制台的打印语句 (4) 我试图填充的屏幕部分在 javascript/drills.js.coffee 中

资产/javascript/drills.js.coffee

changeAssump = (data)-> 
   console.log("-6-  in changeAssump  ")
    console.log("-6- print data from call")
    console.log data
    # this is what I can't do
    # trying to get values Drills controller action user_valuation_selected to be displayed
    # on the screen partial assumption_params   
    # e.g ('#evaluation_assumption_resource_estimation_gas').val(resource_estimation_gas)
    # where resource_estimation_gas is coming from the returned object

# this ok (I think) and calling above
changeAssumptions = (drill_id)->
   data = $('#user_save_name_evaluation_assumption_id').serialize()
   console.log("-4- in changeAssumptions")
   console.log data
   $.ajax
    url:"/drills/#{drill_id}/user_valuation_selected.json"
    type:"post"
    dataType: 'json'   # data type of response
    data: data
    success: (data,success,xhr)->
        changeAssump data

演习.controller.rb

def user_valuation_selected
        @userassum = EvaluationAssumption.find(params[:user_save_name][:evaluation_assumption_id])
          respond_to do |format|
        format.json { render :json => @userassum }
    end
end

开发者工具 -> 控制台 -> 上面打印语句的输出

-> 对象 {created_at: "2013-09-26T00:36:34Z", Drill_id: 1, id: 51, in_ground_value_gas: 1, in_ground_value_oil: 5…}

当通过单击箭头 (->) 展开时,这是我无法在 javascript/drills.js.coffee 中访问的数据,例如我想要访问 resource_estimation_gas 等

assets/javascript/drills.js.coffee created_at: "2013-09-26T00:36:34Z" Drill_id: 1 id: 51 in_ground_value_gas: 1 in_ground_value_oil: 5 member_profile_id: 1probability_of_success: 11 resource_estimation_gas: 1 resource_estimation_oil: 1.168

画面偏

# as included within main screen
<div class="form assumption" data-drillid="<%= @drill.id %>">
    <div id="id_assumption_params">
            <%= render 'assumption_params' %>
    <div>
    </div>


# part of the partial assumption_params.html.erb

<%= simple_form_for @evaluation_assumption,
    :html => { class: 'infogroup', 
    id: "evaluation_assumption_params"  } do |f| %>
…
…
<%= content_tag :td, f.text_field(:resource_estimation_gas, 
              class: "general ralign"), class: 'data' %>
  <%= content_tag :td, f.text_field(:resource_estimation_oil, 
              class: "general ralign"), class: 'data' %>
4

1 回答 1

0

prefixing the value I want with data gets the derisred result.

For example to get the value for the saved evaluation_assumption resource_estimation_gas, I reference val(data.resource_estimation_gas).

thanks to all those who checked this question

Pierre

于 2013-10-03T08:08:04.203 回答