gpt4 book ai didi

javascript - 尝试 AJAXify 这个 Rails 4 方法调用并收到 500 错误

转载 作者:行者123 更新时间:2023-11-28 01:41:34 25 4
gpt4 key购买 nike

我的 View 中有一个表格:views/uploads/index.html.erb

  other code

<div id="uploads_table">
<table class="table table-striped">
<thead>
<tr>

<th>File name</th>

blah
blah
blah

<th>Actions</th>
</tr>
</thead>
<tbody >
<%= render (@uploads) %> <%# partial _upload.html.erb %>
</tbody>

这是部分“_upload.html.erb”

 <tr id= "uploads_table_rows">

<td> <%= upload.sourcedata_file_name%></td>
<% path_arr = upload.f_path.split("\/")%>


blah
blah
blah

<%end%>
<td><%= render 'button', :upload => upload%></td>
</tr>

当后台作业运行并且表中的值发生变化时,我尝试更新此表。

为此,我进行了以下设置:1)在我的 upload_controller.rb 中,我有以下方法:

 before_action :set_upload, only: [:show, :edit, :update, :destroy]

#refresh uploads table in uploads/index.html.erb
def refresh_table
@uploads = Upload.all
@uploads.each do |u|
if(File.exist?(u.f_path))
puts "File #{u.sourcedata_file_name} exists"
else
puts "File #{u.sourcedata_file_name} has been removed"
u.update!(:status => "-1")
end
end
@uploads = Upload.all
respond_to do |format|
format.js
end
end


private
# Use callbacks to share common setup or constraints between actions.
def set_upload
@upload = Upload.find(params[:upload][:id])
end

# Only allow a trusted parameter "white list" through.
def upload_params
params.require(:upload).permit(:id, :sourcedata, :task_id, :status, :f_path, :job)
end

2)在我的 uploads.js.coffee 中我添加了这段代码

 $(document).ready ->

# will call refresh_table every 1 second
setInterval refresh_table, 1000

refresh_table = ->
$.ajax url: "/uploads_controller/refresh_table"

3)我使用以下代码添加了一个文件“refresh_table.js.erb”

  $('#uploads_table').html("#{escape_javascript(render 'upload', data:@uploads)}");

4)最后我更新了我的路由文件,将此行作为最后一行:

 get 'uploads_controller/refresh_table'

我重新启动了服务器并启动了更新表值的后台作业。我在 chrome 中运行了 javascript 控制台,我看到的只是每 1 秒这些行

 GET http://localhost:3000/uploads_controller/refresh_table 404 (Not Found) 
jquery.js?
body=1:8707

我不确定出了什么问题。我是 AJAX 新手,所以我想知道是否有人可以帮我解决这个问题。谢谢

<小时/>

更新:尝试下面发布的解决方案后,这就是我的 paths.rb 的样子:

 blah 
blah


resource :uploads do
member do
post "parse"
get "refresh_table", to: "refresh_table"
end
end


blah
blah

这是我的 Chrome 浏览器中看到的“网络”选项卡的图像: enter image description here

控制台选项卡如下所示: enter image description here

服务器控制台输出:

 Started GET "/uploads/refresh_table" for 127.0.0.1 at 2014-01-01 10:24:42 -0500
Processing by UploadsController#show as */*
Parameters: {"id"=>"refresh_table"}
Completed 500 in 1ms

NoMethodError - undefined method `[]' for nil:NilClass:
app/controllers/uploads_controller.rb:89:in `set_upload'

网络预览选项卡: enter image description here

预览选项卡: enter image description here

最佳答案

这是你的问题:

$.ajax url: "/uploads_controller/refresh_table"

-> GET http://localhost:3000/uploads_controller/refresh_table 404 (Not Found)

应该是:

$.ajax url: "/uploads/refresh_table"

您的路线应该是:

resources :uploads do 
get "refresh_table", to: "refresh_table"
end
<小时/>

Ajax

Ajax 的工作原理如下:

User request -> JS -> Ajax -> Rails
User view <- JS <- Ajax <- Rails

它基本上是一个“伪浏览器”

它接受用户执行的操作/请求(单击链接,在 View 中执行某些操作),然后使用 Javascript 处理该请求

Ajax 被称为 Asynchronous Javascript And XML因为它代表您发送请求。让大多数人感到困惑的是,Ajax 与 Rails 完全脱节,并且必须使用与“普通”浏览器相同的路由结构

最好将其视为传送系统 - 您在客户端的请求将通过 Ajax“传送”到 Rails。但是,就像传送系统一样,您仍然需要知道坐标并有一个要连接的端点

关于javascript - 尝试 AJAXify 这个 Rails 4 方法调用并收到 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20864679/

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