提问人:banditKing 提问时间:1/3/2014 最后编辑:banditKing 更新时间:1/3/2014 访问量:16375
获取操作控制器未知格式错误 - 对于 rails 4 + 不显眼的 javascript 和 AJAX 调用
Getting Action controller unknown format error- for rails 4 + unobtrusive javascript and AJAX call
问:
我得到了以下设置:
uploads_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
然后我有包含以下行的routes.rb:
match 'uploads/refresh_table', to: 'uploads#refresh_table', via: :get
resources :uploads
上传.js.coffee:
$(document).ready ->
# will call refresh_table every 1 second
setInterval refresh_table, 5000
refresh_table = ->
$.ajax url: "/uploads/refresh_table"
视图/上传/索引.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>
部分浏览/上传/_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>
然后我有 views/uploads/refresh_table.js.erb
$('#uploads_table_rows').html("<%=escape_javascript(render(@uploads)) %>");
当我导航到
some_url/uploads/refresh_table
我在浏览器中收到错误:
ActionController::UnknownFormat
Request parameters
{"controller"=>"uploads", "action"=>"refresh_table"}
服务器输出为:
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:
我一直在尝试调试它并使用各种方法使其工作 2 天,但没有成功。最后,我知道这里的路由有效,但不知何故,现在没有唤起 javascript 视图。
有人可以帮忙吗?谢谢
答:
5赞
arun15thmay
1/3/2014
#1
在 uploads.js 中尝试以下代码
$.ajax url: "/uploads/refresh_table", format: 'js'
您的操作仅处理 ajax 请求。但是 ajax 请求在服务器端作为 HTML 请求接收,这意味着它不是 ajax 请求。
评论