获取动作控制器未知格式错误 - 对于 rails 4 + 不显眼的 javascript 和 AJAX 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20895506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:34:10  来源:igfitidea点击:

Getting Action controller unknown format error- for rails 4 + unobtrusive javascript and AJAX call

javascriptruby-on-railsajaxunobtrusive-javascriptactioncontroller

提问by banditKing

Ive got the following setup:

我有以下设置:

uploads_controller.rb:

上传_controller.rb:

 class UploadsController < ApplicationController
    before_action :set_upload, only: [:show, :edit, :update, :destroy] 

   # GET /uploads
   def index
     @uploads = Upload.all
 update_file_status
 @uploads = Upload.all
   end

  # GET /uploads/1
  def show
  end

  # GET /uploads/new
  def new
puts "Running uploads/new"
   @upload = Upload.new()
  end

 # GET /uploads/1/edit
def edit
end

# POST /uploads
def create

@upload = Upload.new(upload_params)
if @upload.save
    @upload.update!(:status => "1")
    @upload.update!(:f_path => "#{@upload.sourcedata.path}")
    redirect_to uploads_url, notice: "Upload for #{@upload.task.name} was successfully created with file #{@upload.sourcedata_file_name}."
    else
       redirect_to tasks_url, alert: "*** ERROR *** Upload for #{@upload.task.name} did not go through successfully. #{@upload.errors.messages}"
    end
  end

  # PATCH/PUT /uploads/1
def update
    puts "Update method in Uploads controller received params = #{params.inspect}"
    puts "params[:upload][:job] = #{params[:upload][:job].inspect}"
    if (params[:upload][:job] == "parse")
        puts "Passed params[:job]== \"parse\" "

        redirect_to uploads_url, notice: "Parsing data from #{@upload.sourcedata_file_name}....."
        @upload.delay.add_data_to_DB()
    else
        if @upload.update(upload_params)
            redirect_to uploads_url, notice: "#{@upload.sourcedata_file_name} was updated"
        else
            redirect_to uploads_url, notice: "ERRRO #{@upload.sourcedata_file_name} could NOT be updated"
        end
    end
end


  # DELETE /uploads/1
  def destroy
     @upload.destroy
redirect_to uploads_url, notice: 'Couldnt parse file #{@upload.sourcedata_file_name}'
  end

# GET /uploads/refresh_table
def refresh_table
    @uploads = Upload.all
    update_file_status
    @uploads = Upload.all
    respond_to do |format|
        format.js # template marked refresh_table.js.erb will be evoked
        #format.html {redirect_to uploads_url}          
    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(:sourcedata, :task_id, :status, :f_path, :job)
   end

def update_file_status
@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
end

   end

Then I have routes.rb containing these lines:

然后我有包含这些行的 routes.rb :

 match 'uploads/refresh_table', to: 'uploads#refresh_table', via: :get 
 resources :uploads

uploads.js.coffee:

上传.js.coffee:

 $(document).ready ->
    # will call refresh_table every 1 second
    setInterval refresh_table, 5000

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

views/uploads/index.html.erb

意见/上传/index.html.erb

 <table class="table table-striped">
   <thead>
     <tr>

  <th>File name</th>
  <th>Upload Date, Time</th>
  <th>File Type</th>
  <th>Program Dates in file</th>
  <th>Total Rows</th>
  <th>Rows read</th>
  <th>Completed</th>
  <th>Status</th>
  <th>Actions</th>
     </tr>
   </thead>
   <tbody>
     <%= render (@uploads)%> <%# partial _upload.html.erb %> 
   </tbody>

 </div>
 </table>

partial views/uploads/_upload.html.erb

部分视图/上传/_upload.html.erb

  <tr id= "uploads_table_rows">

    <td> <%= upload.sourcedata_file_name%></td>
         <% path_arr = upload.f_path.split("\/")%>
    <td><%= path_arr[-3..-2].join(", ").gsub!("_", "\:")%></td>
    <td> <%= upload.sourcedata_content_type%></td>
    <td> <%= upload.date_ranges.pluck(:date_range).join(", ")%>
    <td> <%= upload.total_rows%></td>
    <td> <%= upload.rows_completed%></td>
    <td> <%= number_to_percentage(upload.percentage_complete, :precision => 0)%></td>
    </td>
    <%if (upload.status == 0 ) %>
        <td>Error </td>
    <%elsif (upload.status == 1 ) %>
        <td>File saved on server</td>
    <%elsif (upload.status == 3 ) %>
        <td>File is being parsed </td>  
    <%elsif (upload.status == 2 ) %>
        <td>File data already in Database </td> 
    <%else%>
        <td>File doesn't exist on Server</td>   
    <%end%>
        <td><%= render 'button', :upload => upload%></td>
  </tr>

then I have views/uploads/refresh_table.js.erb

然后我有视图/上传/refresh_table.js.erb

 $('#uploads_table_rows').html("<%=escape_javascript(render(@uploads)) %>");

When I navigate to

当我导航到

  some_url/uploads/refresh_table

I get an error in the browser:

我在浏览器中收到错误消息:

ActionController::UnknownFormat
 Request parameters 
 {"controller"=>"uploads", "action"=>"refresh_table"}

server output is:

服务器输出是:

 Started GET "/uploads/refresh_table" for 127.0.0.1 at 2014-01-02 21:10:26 -0500
 Processing by UploadsController#refresh_table as HTML
  Upload Load (0.3ms)  SELECT `uploads`.* FROM `uploads`

 Completed 500  in 2ms

 ActionController::UnknownFormat - ActionController::UnknownFormat:

Ive been trying to debug this and make this work for 2 days with various approaches but unsuccessfully. Finally I know that the routing here works but somehow now the javascript view is not being evoked.

我一直在尝试调试它并使用各种方法使其工作 2 天,但没有成功。最后我知道这里的路由有效,但不知何故现在没有调用 javascript 视图。

Can someone please assit. Thanks

有人可以帮忙吗?谢谢

采纳答案by arun15thmay

Try the following code in uploads.js.coffee

在uploads.js.coffee中尝试以下代码

$.ajax url: "/uploads/refresh_table", format: 'js'

Your action handles only ajax requests. But the ajax request is being received at the server end as HTML request meaning it is not ajax request.

您的操作仅处理 ajax 请求。但是 ajax 请求是作为 HTML 请求在服务器端接收的,这意味着它不是 ajax 请求。