Чего я хочу достичь:
Пользователь выбирает значение из тега выбора (class = "repeatability_weight"
, который ищет данные соответствующего элемента из базы данных и затем возвращаетэти данные в строке (class = "repeatability_measured"
, но при работе будет больше) этой таблицы без обновления всей страницы.
Я просмотрел других вопросов и посмотрел видео на railscast и вот где у меня есть, но я не могу переступить черту с ним.
Контроллер
def find_weight
@applied_load_weight = Weight.find(params[:weight_id])
respond_to do |format|
format.html
format.js
end
end
Просмотр
<table class="table table-hover table-sm">
<thead>
<th colspan="1"></th>
<th>Nominal</th>
<th>Required</th>
<th>Measured</th>
</thead>
<tbody>
<tr>
<td>Applied Load</td>
<td><%= select_tag "repeat_weight", options_from_collection_for_select(Weight.where("weightset_id = ?",@calibration_header.weightset ),:id, :name ),
{:multiple => true, :class => 'chosen-select repeatability_weight'} %></td>
<td><%= number_field_tag 'required', nil, min: 1 %></td>
<td class ="repeatability_measured"></td>
</tr>
<tr>
<td>Zero Applied Offset Load</td>
<td><%= select_tag "repeat_weight", options_from_collection_for_select(Weight.where("weightset_id = ?",@calibration_header.weightset ),
:id, :name ), {:multiple => true, :class => 'chosen-select repeatability_weight'} %></td>
<td><%= number_field_tag 'required', nil, min: 1 %></td>
<td class ="repeatability_measured"></td>
</tr>
</tbody>
</table>
Javascript
$(".repeatability_weight").on('change', function(){
var input = $(this)
var val = input.val()? input.val() : 0;
var applied_weight = val
window.alert(applied_weight);
$.ajax({
url: '<%= weightsets_weight_find_path %>',
type: 'GET',
data: {'weight_id': applied_weight},
})
var res = $(".resolution").data("resolution")
var measured = measured.toFixed(res)
$(this).closest('tr').find('.repeatability_measured')[0].textContent = measured;
})
Мой аякс звонит набазы данных и выбирает правильные данные. Я изо всех сил пытаюсь получить данные из @ apply_weight.measured к измеряемой переменной, чтобы остальная часть javascript могла обработать ее.
Любая помощь?