簡體   English   中英

在javascript / js.coffee中訪問json數據

[英]Accessing json data within javascript/js.coffee

在資產/ 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

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等

asset / 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可能性_成功: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' %>

給我想要的值加上數據前綴會得到令人討厭的結果。

例如,要獲取已保存的valuation_assumption resource_estimation_gas的值,我引用val(data.resource_estimation_gas)。

感謝所有檢查此問題的人

皮埃爾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM