Ruby on Rails -in coffeescript how do I access data returned from controller -



Ruby on Rails -in coffeescript how do I access data returned from controller -

ruby-on-rails application using javascript/coffee access returned info re-display drop-down list on view.

app/assets/javascripts

$('document').ready -> if $('#x_eval_assum').length == 1 $('#x_eval_assum') # evaluation assumption saved .submit (event) -> event.preventdefault() info = $("#x_eval_assum").serialize() user_save_name = data.user_save_name drill_id = $('.form.assumption').attr('data-drillid') $.post "/drills/#{drill_id}/discovery_target_saved.json", data, (res)-> console.log res # response shown below # line causing errors - how access whats in res assumption in res.assumptions # $(select).append(<option val="id"

using developer -> tools view res (e.g. data) returned coffeescript controller

data: array[12] 0: object id: "c-22" name: "gas, ado" index: 0 ... 1: object ...

app/controllers/drills

... def discovery_target_saved @evaluation_assumption = evaluationassumption.new(evaluation_assumption_params) load_evaluation_assumption_selections render json: {data: @selections} end

...

that part of view displaying discovery targets

... <td id="discovery_targets" data-targets="<%= @probability_json %>"> <%= select("name", "id", @selections.collect {|r| [ r["name"], r["id"] ] }, { :include_blank => false }) %> </td>

thanks - pierre

look @ controller:

render json: {data: @selections}

that means you're returning hash contains data key value array of something. @ what's in console:

data: array[12] 0: object id: "c-22" name: "gas, ado" index: 0 ...

that means res.data array of 12 items , each item has id, name, ... properties. means want to:

for assumption in res.data ...

do access data.

ruby-on-rails coffeescript

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -