我试图在表单上调用部分内容并进行部分更新而不刷新表单的其余部分。
部分在我的投诉表格中被调用
<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控制器中的销毁def是
def destroy
@document_history.destroy
respond_to do |format|
format.js {}
format.json { render :show, status: :ok, location: @document_history }
end
end
我有一个\ app \ views \ document_histories \ destroy.js.erb的js文件
$('#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)
答案 0 :(得分:2)
DocumentHistoriesController中的ActionController :: UnknownFormat#destroy
您需要在链接中添加remote: true
,以便向控制器发送JS
请求。
<%= 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. "} %>
现在,在控制器操作中使用format.js
处理请求。