我正在尝试调用表单上的部分内容并进行部分更新,而不刷新表单的其余部分。
在我的投诉表中调用了部分内容
<div id="refresh_assigned">
<%= render :partial => '/document_histories/history_for_complaint_form' %>
</div>
我正在\document_histories_history_for_complaint_form.html.erb 中调用删除
<td><%= link_to 'Delete', history, method: :delete, data: { confirm: "Are you sure that you want to remove this users assignment to this complaint? \n \n The user will NOT be notified via email. "} %></td>
我在 document_histories Controller 中的销毁定义是
def destroy
@document_history.destroy
respond_to do |format|
format.js {}
format.json { render :show, status: :ok, location: @document_history }
end
end
我有一个js文件\app\views\document_histories\destroy.js.erb
$('#refresh_assigned').html("<%= escape_javascript(render(:partial => 'document_histories/history_for_complaint_form')).html_safe %>");
我收到的错误是
ActionController::UnknownFormat in DocumentHistoriesController#destroy
ActionController::UnknownFormat
Rails.root: C:/Users/cmendla/RubymineProjects/internal_complaints
Application Trace | Framework Trace | Full Trace
app/controllers/document_histories_controller.rb:209:in `destroy'
第209行是respond_to do |format|
<小时/>这是我在进行 Pavan 建议的更改后尝试删除时的日志。看起来我在尝试刷新 div 时遇到了 Nil 类错误
Started DELETE "/document_histories/364" for ::1 at 2016-02-23 09:22:21 -0500
Processing by DocumentHistoriesController#destroy as JS
Parameters: {"id"=>"364"}
[1m[35mUser Load (1.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mDocumentHistory Load (0.0ms)[0m [1mSELECT "document_histories".* FROM "document_histories" WHERE "document_histories"."id" = ? LIMIT 1[0m [["id", 364]]
[1m[35m (0.0ms)[0m begin transaction
[1m[36mSQL (9.0ms)[0m [1mDELETE FROM "document_histories" WHERE "document_histories"."id" = ?[0m [["id", 364]]
[1m[35m (11.0ms)[0m commit transaction
Rendered document_histories/_history_for_complaint_form.html.erb (2.0ms)
Rendered document_histories/destroy.js.erb (5.0ms)
Completed 500 Internal Server Error in 42ms (ActiveRecord: 21.0ms)
ActionView::Template::Error (undefined method `each' for nil:NilClass):
8: <th> Remarks</th>
9: </tr>
10:
11: <% @document_histories.each do |history| %>
12:
13:
14:
app/views/document_histories/_history_for_complaint_form.html.erb:11:in `_app_views_document_histories__history_for_complaint_form_html_erb___652294712_44088072'
app/views/document_histories/destroy.js.erb:1:in `_app_views_document_histories_destroy_js_erb___293771988_76518024'
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms)
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (19.0ms)
最佳答案
ActionController::UnknownFormat in DocumentHistoriesController#destroy
您需要在链接中添加 remote: true
才能将 JS
请求发送到 Controller 。
<%= link_to 'Delete', history, remote: true, method: :delete, data: { confirm: "Are you sure that you want to remove this users assignment to this complaint? \n \n The user will NOT be notified via email. "} %>
现在请求由 Controller 操作中的 format.js
处理。
关于javascript - DocumentHistoriesController# 中的 ActionController::UnknownFormat 和 format.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35579346/