简体   繁体   English

尝试AJAXify这个rails 4方法调用并得到500错误

[英]Trying to AJAXify this rails 4 method call and getting 500 error

I have a table in my view: views/uploads/index.html.erb 我的视图中有一个表: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>

Here is the partial '_upload.html.erb' 这是部分“ _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>

Im trying to update this table as the background jobs run and the values in the tables change. 我试图在后台作业运行并且表中的值更改时更新此表。

For this I have the following setup: 1) In my upload_controller.rb I have the followin method: 为此,我有以下设置: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) In my uploads.js.coffee I added this code 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) I added a file 'refresh_table.js.erb' with the following code 3)我用以下代码添加了文件“ refresh_table.js.erb”

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

4) Finally I updated my routes file with this line as the last line: 4)最后,我用这行作为最后一行更新了路由文件:

 get 'uploads_controller/refresh_table'

The I restarted the server and started the background job that updates the table values. 我重新启动了服务器,并启动了更新表值的后台作业。 I ran the javascript console in chrome and all I see is a these lines every 1 second 我在chrome中运行了javascript控制台,我看到的是每1秒显示这些行

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

Im not sure whats wrong. 我不知道怎么了。 im new to AJAX and so I was wondering if someone can give me a hand with this. 我是AJAX的新手,所以我想知道是否有人可以帮我这个忙。 Thanks 谢谢


UPDATE: After trying the solution posted below this is what my routes.rb looks like: 更新:尝试下面发布的解决方案后,这是我的route.rb的样子:

 blah 
 blah


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


 blah
 blah

And here is an image of the Network tab as seen in my chrome browser : 这是在Chrome浏览器中看到的“网络”标签的图像: 在此处输入图片说明

The console tab looks like this: 控制台选项卡如下所示: 在此处输入图片说明

Server Console out put: 服务器控制台输出:

 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'

Preview tab of network: 网络的预览标签: 在此处输入图片说明

Preview tab: 预览标签: 在此处输入图片说明

Here's your problem: 这是您的问题:

$.ajax url: "/uploads_controller/refresh_table"

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

It should be: 它应该是:

$.ajax url: "/uploads/refresh_table"

Your routes should be: 您的路线应为:

resources :uploads do 
    get "refresh_table", to: "refresh_table"
end

Ajax 阿贾克斯

Ajax works like this: Ajax的工作方式如下:

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

It's basically a "pseudo browser" 基本上是“伪浏览器”

It takes an action / request performed by the user (click a link, do something in the view), and you handle that request with Javascript 它执行用户执行的操作/请求(单击链接,在视图中执行某些操作),然后使用Javascript处理该请求

Ajax is called Asynchronous Javascript And XML because it sends requests on your behalf. Ajax之所以称为异步Java语言和XML,是因为它代表您发送请求。 What confuses most people is that Ajax is completely disconnected from Rails, and has to use the same routing structure as the "normal" browser 令大多数人感到困惑的是,Ajax与Rails完全断开,并且必须使用与“普通”浏览器相同的路由结构

It's best to think of it as a teleport system - your request on the client-side is "teleported" with Ajax to Rails. 最好将其视为一个传送系统 -您在客户端的请求是通过Ajax to Rails“传送”的。 However, like a teleport system, you still need to know the co-ordinates & have an endpoint to connect to 但是,就像一个传送系统一样,您仍然需要了解坐标并有一个端点可以连接到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM