gpt4 book ai didi

javascript - Rails 操作以状态 200 运行,但未访问模板且未触发 jQuery

转载 作者:行者123 更新时间:2023-11-28 06:21:03 25 4
gpt4 key购买 nike

在下面的代码中, "$('#movesindexrowstable').DataTable({..."永远不会触发。但是, "$('#movesindexlotstable').DataTable({..."工作得很好。

有趣的是, Controller “移动”操作“indexrows”呈现状态为 200,但模板不会执行,因为其中的断点不会触发。当然,如果未执行模板,则不会处理 ID movesindexrowstable,这解释了为什么 jQuery 代码不会触发。

渲染代码 MovesIndexlotsDatatable 和 MovesIndexrowsDatatable 返回有效的 JSON。当然,即使没有,也不应该影响渲染对模板的使用吧?

考虑到可能涉及涡轮链接,我将第一行更改为使用 page:change 而不是常见的“$(document).ready(function () {”以符合其要求。这没有帮助,也没有帮助确实添加了数据无涡轮链接。

我通过 jquery-datatables-rails 使用 DataTables 和 AJAX 来显示表中的 JSON 数据。但是,DataTables 永远不会获得控制权,因为选择器永远不会被识别,并且该函数不会触发。

这让我很困惑,希望得到任何帮助。谢谢。

编辑:我相信下图即使不是解决方案,也能隔离问题。 Moves#Indexlots 的初始 GET 呈现模板,后续 POST 提供数据但不呈现模板。到目前为止,我的编码仅包括 Moves#Indexrows 数据的 POST。看来我需要说服 POST 来渲染模板,或者找到一种替代方法,例如通过 GET?如果是这样,我想要有关如何做到这一点的建议。谢谢。

编辑:看起来像/一个答案? Render an HTML partial inside a JSON request enter image description here

编辑:在 JQ 中,我将“$.post('/moves_indexrows.json'”更改为“$.post('/moves_indexrows.html'”。这导致 POST 呈现预期的结果页面。网络跟踪显示页面大小为 15.6KB。Rails 跟踪显示渲染的 ERB 文件。即使浏览器看到了所有这些,也没有发生页面更改!我真的不知道了解这一点。显示为:

Network Trace shows 15.6KB page status 200, no change in view.

Rails Trace shows all ERB files/partials rendered, no view update.

jQuery/JavaScript 代码:

$(document).on('page:change', function(){

// AJAX DataTable Moves Lots for bulk moves
var lots_active = [];
var movesindexlotstable = $('#movesindexlotstable').DataTable({
responsive: true,
autoWidth: false,
pagingType: 'full',
jQueryUI: false,
processing: true,
serverSide: true,
ajax: {
url: 'moves_indexlots.json',
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data: function (d) {
return JSON.stringify(d);
}
},
columns: [
{"data": "name", className: null}
],
rowCallback: function (row, data) {
if ($.inArray(data.DT_RowId, lots_active) !== -1) {
$(row).addClass('active');
}
}
});
$('#movesindexlotstable tbody').on('click', 'tr', function () {
var id = this.id;
var index = $.inArray(id, lots_active);
if (index === -1) {
lots_active.push(id);
} else {
lots_active.splice(index, 1);
}
$(this).toggleClass('active');
});
// Ajax moves, select lots
$('#movesindexlotsbutton').on('click', function () {
var source = 'lots';
$.post('/moves_indexrows.json',
{
commit: 'Moves Lots',
source: source,
active: lots_active
}
);
return false;
});

// AJAX DataTable Moves Rows within Lots for bulk moves
var rows_active = [];
var movesindexrowstable = $('#movesindexrowstable').DataTable({
responsive: true,
autoWidth: false,
pagingType: 'full',
jQueryUI: false,
processing: true,
serverSide: true,
ajax: {
url: 'moves_indexrows.json',
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data: function (d) {
return JSON.stringify(d);
}
},
columns: [
{"data": "name", className: null}
],
rowCallback: function (row, data) {
if ($.inArray(data.DT_RowId, rows_active) !== -1) {
$(row).addClass('active');
}
}
});
$('#movesindexrowstable tbody').on('click', 'tr', function () {
var id = this.id;
var index = $.inArray(id, rows_active);
if (index === -1) {
rows_active.push(id);
} else {
rows_active.splice(index, 1);
}
$(this).toggleClass('active');
});
// Ajax moves, select rows from selected lots
$('#movesindexrowsbutton').on('click', function () {
var source = 'rows';
$.post('/moves_indexcars.json',
{
commit: 'Moves Rows',
source: source,
active: rows_active
}
);
return false;
});

});

RoR代码:

class MovesController < ApplicationController

before_action :check_if_associate

def indexlots
respond_to do |format|
format.html
format.json { render json: MovesIndexlotsDatatable.new(view_context) }
end
end

def indexrows
lots = Array.new
params[:active].map { |lot| id = lot[4..(lot.length-1)].to_i; lots << Lot.find(id); }
respond_to do |format|
format.html
format.json { render json: MovesIndexrowsDatatable.new(view_context, lots) }
end
end

end

moves/indexlots.html.erb

<div class="row">
<%= render partial: 'indexlots', layout: 'layouts/kac_label', locals: {title: 'Moves Cars - Lots' } %>
</div>

moves/indexrows.html.erb

<div class="row">
<%= render partial: 'indexrows', layout: 'layouts/kac_label', locals: { title: 'Moves Cars - Rows' } %>
</div>

移动/_indexlots.html.erb

<div class="form-group span8">
<table id="movesindexlotstable" class="display dt-responsive no-wrap table-striped" cellspacing="0" width="80%">
<thead>
<tr>
<th class="all"><b>Lot</b></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class='break_line'></div>
<input type="submit" name="commit" value="Select Lots" id="movesindexlotsbutton" class="btn btn-primary kc-wide" data-no-turbolink data-disable-with="Processing">
<%= link_to 'Home', '/', class: 'btn btn-primary kc-wide' %>
</div>

moves/_indexrows.html.erb

<div class="form-group span8">
<table id="movesindexrowstable" class="display dt-responsive no-wrap table-striped" cellspacing="0" width="80%">
<thead>
<tr>
<th class="all"><b>Rows</b></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class='break_line'></div>
<input type="submit" name="commit" value="Select Rows" id="movesindexrowsbutton" class="btn btn-primary kc-wide" data-no-turbolink data-disable-with="Processing">
<%= link_to 'Home', '/', class: 'btn btn-primary kc-wide' %>
</div>

日志:

...
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 127.0.0.1:3000, CTRL+C to stop


Started GET "/moves_indexlots" for 127.0.0.1 at 2016-02-20 16:58:32 -0500
ActiveRecord::SchemaMigration Load (1.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by MovesController#indexlots as HTML
Company Load (0.5ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 LIMIT 1 [["prefix", "ucf"]]
Company Load (0.0ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 ORDER BY "companies"."id" ASC LIMIT 1 [["prefix", "ucf"]]
CACHE (0.0ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 LIMIT 1 [["prefix", "ucf"]]
Associate Load (1.0ms) SELECT "associates".* FROM "associates" WHERE "associates"."company_id" = $1 AND "associates"."id" = $2 LIMIT 1 [["company_id", 1], ["id", 74]]
Role Load (0.5ms) SELECT "roles".* FROM "roles" INNER JOIN "associates_roles" ON "roles"."id" = "associates_roles"."role_id" WHERE "associates_roles"."associate_id" = $1 AND (((roles.name = 'associate') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["associate_id", 74]]
CACHE (0.0ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 LIMIT 1 [["prefix", "ucf"]]
Rendered moves/_indexlots.html.erb (8.0ms)
Rendered moves/indexlots.html.erb within layouts/application (17.5ms)
Action#Controller is indexlots#moves
Rendered layouts/_shim.html.erb (1.0ms)
Role Load (0.5ms) SELECT "roles".* FROM "roles" INNER JOIN "associates_roles" ON "roles"."id" = "associates_roles"."role_id" WHERE "associates_roles"."associate_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["associate_id", 74]]
Rendered layouts/_navigation_links.html.erb (12.5ms)
Rendered layouts/_navigation.html.erb (15.0ms)
Rendered layouts/_messages.html.erb (1.0ms)
Completed 200 OK in 2386ms (Views: 2103.0ms | ActiveRecord: 7.5ms)


Started POST "/moves_indexlots.json" for 127.0.0.1 at 2016-02-20 16:58:35 -0500
Processing by MovesController#indexlots as JSON
Parameters: {"draw"=>1, "columns"=>[{"data"=>"name", "name"=>"", "searchable"=>true, "orderable"=>true, "search"=>{"value"=>"", "regex"=>false}}], "order"=>[{"column"=>0, "dir"=>"asc"}], "start"=>0, "length"=>10, "search"=>{"value"=>"", "regex"=>false}, "move"=>{"draw"=>1, "columns"=>[{"data"=>"name", "name"=>"", "searchable"=>true, "orderable"=>true, "search"=>{"value"=>"", "regex"=>false}}], "order"=>[{"column"=>0, "dir"=>"asc"}], "start"=>0, "length"=>10, "search"=>{"value"=>"", "regex"=>false}}}
Company Load (0.5ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 LIMIT 1 [["prefix", "ucf"]]
Company Load (1.0ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 ORDER BY "companies"."id" ASC LIMIT 1 [["prefix", "ucf"]]
CACHE (0.0ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 LIMIT 1 [["prefix", "ucf"]]
Associate Load (0.5ms) SELECT "associates".* FROM "associates" WHERE "associates"."company_id" = $1 AND "associates"."id" = $2 LIMIT 1 [["company_id", 1], ["id", 74]]
Role Load (1.0ms) SELECT "roles".* FROM "roles" INNER JOIN "associates_roles" ON "roles"."id" = "associates_roles"."role_id" WHERE "associates_roles"."associate_id" = $1 AND (((roles.name = 'associate') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["associate_id", 74]]
(1.5ms) SELECT COUNT(*) FROM "lots" WHERE "lots"."company_id" = $1 [["company_id", 1]]
CACHE (0.0ms) SELECT COUNT(*) FROM "lots" WHERE "lots"."company_id" = $1 [["company_id", 1]]
Lot Load (0.5ms) SELECT "lots".* FROM "lots" WHERE "lots"."company_id" = $1 ORDER BY name asc LIMIT 10 OFFSET 0 [["company_id", 1]]
Completed 200 OK in 195ms (Views: 52.5ms | ActiveRecord: 7.5ms)


Started POST "/moves_indexrows.json" for 127.0.0.1 at 2016-02-20 16:58:41 -0500
Processing by MovesController#indexrows as JSON
Parameters: {"commit"=>"Moves Lots", "source"=>"lots", "active"=>["Row_64", "Row_29"]}
Company Load (0.5ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 LIMIT 1 [["prefix", "ucf"]]
Company Load (0.0ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 ORDER BY "companies"."id" ASC LIMIT 1 [["prefix", "ucf"]]
CACHE (0.0ms) SELECT "companies".* FROM "companies" WHERE "companies"."prefix" = $1 LIMIT 1 [["prefix", "ucf"]]
Associate Load (0.5ms) SELECT "associates".* FROM "associates" WHERE "associates"."company_id" = $1 AND "associates"."id" = $2 LIMIT 1 [["company_id", 1], ["id", 74]]
Role Load (0.5ms) SELECT "roles".* FROM "roles" INNER JOIN "associates_roles" ON "roles"."id" = "associates_roles"."role_id" WHERE "associates_roles"."associate_id" = $1 AND (((roles.name = 'associate') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["associate_id", 74]]
Lot Load (0.5ms) SELECT "lots".* FROM "lots" WHERE "lots"."company_id" = $1 AND "lots"."id" = $2 LIMIT 1 [["company_id", 1], ["id", 64]]
Lot Load (0.5ms) SELECT "lots".* FROM "lots" WHERE "lots"."company_id" = $1 AND "lots"."id" = $2 LIMIT 1 [["company_id", 1], ["id", 29]]
(1.0ms) SELECT COUNT(*) FROM "rows" WHERE "rows"."company_id" = $1 [["company_id", 1]]
Row Load (0.0ms) SELECT "rows".* FROM "rows" WHERE "rows"."company_id" = $1 AND (lot_id = 64) ORDER BY name asc [["company_id", 1]]
Row Load (0.5ms) SELECT "rows".* FROM "rows" WHERE "rows"."company_id" = $1 AND (lot_id = 29) ORDER BY name asc [["company_id", 1]]
(0.5ms) SELECT COUNT(*) FROM "rows" WHERE "rows"."company_id" = $1 AND "rows"."id" IN (186, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144) [["company_id", 1]]
CACHE (0.0ms) SELECT "rows".* FROM "rows" WHERE "rows"."company_id" = $1 AND (lot_id = 64) ORDER BY name asc [["company_id", 1]]
CACHE (0.0ms) SELECT "rows".* FROM "rows" WHERE "rows"."company_id" = $1 AND (lot_id = 29) ORDER BY name asc [["company_id", 1]]
Row Load (0.5ms) SELECT "rows".* FROM "rows" WHERE "rows"."company_id" = $1 AND "rows"."id" IN (186, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144) LIMIT 10 OFFSET 0 [["company_id", 1]]
CACHE (0.0ms) SELECT COUNT(*) FROM "rows" WHERE "rows"."company_id" = $1 AND "rows"."id" IN (186, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144) [["company_id", 1]]
Completed 200 OK in 213ms (Views: 86.3ms | ActiveRecord: 6.9ms)

MovesIndexlotsDatatable JSON:

"{"draw":1,"recordsTotal":57,"recordsFiltered":57,"data":[{"DT_RowId":"Row_64","name":"auction"},{"DT_RowId":"Row_29","name":"Buying-Ctr"},{"DT_RowId":"Row_30","name":"Buying-Ctr-Corrider A-H"},{"DT_RowId":"Row_27","name":"Buying-Ctr-Corrider B-G"},{"DT_RowId":"Row_31","name":"Buying-Ctr-Corrider C-F"},{"DT_RowId":"Row_32","name":"Buying-Ctr-Corrider D-E"},{"DT_RowId":"Row_36","name":"Buying-Ctr-Corrider E-L"},{"DT_RowId":"Row_35","name":"Buying-Ctr-Corrider F-K"},{"DT_RowId":"Row_34","name":"Buying-Ctr-Corrider G-J"},{"DT_RowId":"Row_33","name":"Buying-Ctr-Corrider H-I"}]}"

MovesIndexrowsDatatable JSON:

"{"draw":1,"recordsTotal":92,"recordsFiltered":14,"data":[{"DT_RowId":"Row_133","name":" A"},{"DT_RowId":"Row_134","name":" B"},{"DT_RowId":"Row_135","name":" C"},{"DT_RowId":"Row_136","name":" D"},{"DT_RowId":"Row_137","name":" E"},{"DT_RowId":"Row_138","name":" F"},{"DT_RowId":"Row_139","name":" G"},{"DT_RowId":"Row_140","name":" H"},{"DT_RowId":"Row_141","name":" I"},{"DT_RowId":"Row_142","name":" J"}]}"

最佳答案

TL/DR:转换为使用 JavaScript 作为 SPA 和 Rails 作为 HTML/数据微服务。

基本问题似乎是,当某个操作是从 JavaScript 发起时,Rails 并没有完全意识到该操作并对此感到满意。它将运行并使用 HTTP 200 进行渲染,但渲染结果不显示。我运行了跟踪,将标准操作与 JavaScript 启动的操作进行比较,但没有发现处理过程中的差异。我假设有一些内部变量通常在启动时设置,而不是通过 JavaScript 设置,从而导致了此问题。但是,我没有找到。

不过,我确实解决了这个问题。基本上我把这个过程变成了 SPA(单页应用程序)。第一个操作是标准 Rails,但它会在用户第一次交互或单击按钮时将其交给 JavaScript。从那时起,JavaScript 运行该流程,使用 AJAX POST 在一个操作中查询 Rails 的 HTML,在另一个操作中查询数据,从而将其作为一项微服务使用。然后 JavaScript 呈现结果页面,等待再次单击,然后继续。最后一页提供了再次恢复到 Rails 标准处理的按钮。

这就是我的故事,我会坚持下去。谢谢。

PS。我应该提到的是,在一次性的情况下,用 JS 触发一个 Action 对我来说很有效。我的问题是需要经过几层屏幕,每层都通过 JS 触发,因为每次我都有信息从 JS 传递回操作。在一次性情况下,我只是使用以下操作发回控制权:

render js: "window.location.assign(location.origin + '/item')" unless performed?

关于javascript - Rails 操作以状态 200 运行,但未访问模板且未触发 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35529619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com