在javascript / js.coffee中访问json数据

时间:2013-10-03 06:22:57

标签: javascript ruby-on-rails json

在assets / javascript中的js.coffee文件中,我尝试响应从控制器调用中收到的数据失败。在js.coffee文件中使用print语句我可以看到控制器已经返回了正确的数据。在js.coffee文件中,我尝试了许多不同的尝试来访问此数据以将部分放在屏幕上,但未成功。 下面我已经包括了 (1)js,咖啡文件也是如此 (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

drills.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等

资产/ 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:1 probability_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' %>

1 个答案:

答案 0 :(得分:0)

为数据加上前面的数据会得到被破坏的结果。

例如,为了获取已保存的evaluation_assumption resource_estimation_gas的值,我引用val(data.resource_estimation_gas)。

感谢所有检查此问题的人

皮尔