我希望在我的应用程序中对destroy
操作进行AJAX调用,并遵循指南Ajax in Rails 3.1 - A Roadmap的示例#3来执行此操作。我正在使用Rails 3.2.8。
我的AJAX请求工作得很好,destroy
动作就像我期望的那样被调用。
当用户请求destroy
操作的HTML页面时,我想删除该项目,然后将其重定向到另一个页面。
问题是当我插入代码重定向时,HTML和AJAX请求都停止工作。当代码存在时,AJAX请求返回500内部服务器错误,并且HTML请求返回“无法找到SafetyTestsController的动作'show'”。当两行不存在时,AJAX工作正常,HTML仍会返回相同的错误。
查看(取消渲染):
<%= link_to 'Delete', {controller: 'safety_tests', action: 'destroy', id: safety_test.id}, remote: true, method: :delete, confirm: 'Are you sure?', id: "delete_safety_test" %>
查看(已呈现):
<a href="/safety_tests/14" data-confirm="Are you sure?" data-method="delete" data-remote="true" id="delete_safety_test" rel="nofollow">Delete</a>
应用/控制器/ safety_tests_controller.rb
class SafetyTestsController < ApplicationController
respond_to :html, :js
...
def destroy
test = SafetyTest.find(params[:id])
student_id = test.student_id
test.destroy
respond_with(test) do |format|
format.js { render nothing: true }
end
#Two lines below should run for an HTML request but not a Javascript request.
#When uncommented, AJAX request returns 500 Internal Server Error
#flash[:notice] = 'Safety test deleted successfully.'
#redirect_to controller: 'students', action: 'show', id: student_id
end
佣金路线
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
root / dashboard#home
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
trips GET /trips(.:format) trips#index
POST /trips(.:format) trips#create
new_trip GET /trips/new(.:format) trips#new
edit_trip GET /trips/:id/edit(.:format) trips#edit
trip GET /trips/:id(.:format) trips#show
PUT /trips/:id(.:format) trips#update
DELETE /trips/:id(.:format) trips#destroy
safety_tests GET /safety_tests(.:format) safety_tests#index
POST /safety_tests(.:format) safety_tests#create
new_safety_test GET /safety_tests/new(.:format) safety_tests#new
edit_safety_test GET /safety_tests/:id/edit(.:format) safety_tests#edit
safety_test GET /safety_tests/:id(.:format) safety_tests#show
PUT /safety_tests/:id(.:format) safety_tests#update
DELETE /safety_tests/:id(.:format) safety_tests#destroy
medical_forms GET /medical_forms(.:format) medical_forms#index
POST /medical_forms(.:format) medical_forms#create
new_medical_form GET /medical_forms/new(.:format) medical_forms#new
edit_medical_form GET /medical_forms/:id/edit(.:format) medical_forms#edit
medical_form GET /medical_forms/:id(.:format) medical_forms#show
PUT /medical_forms/:id(.:format) medical_forms#update
DELETE /medical_forms/:id(.:format) medical_forms#destroy
parent_permission_forms GET /parent_permission_forms(.:format) parent_permission_forms#index
POST /parent_permission_forms(.:format) parent_permission_forms#create
new_parent_permission_form GET /parent_permission_forms/new(.:format) parent_permission_forms#new
edit_parent_permission_form GET /parent_permission_forms/:id/edit(.:format) parent_permission_forms#edit
parent_permission_form GET /parent_permission_forms/:id(.:format) parent_permission_forms#show
PUT /parent_permission_forms/:id(.:format) parent_permission_forms#update
DELETE /parent_permission_forms/:id(.:format) parent_permission_forms#destroy
teacher_permission_forms GET /teacher_permission_forms(.:format) teacher_permission_forms#index
POST /teacher_permission_forms(.:format) teacher_permission_forms#create
new_teacher_permission_form GET /teacher_permission_forms/new(.:format) teacher_permission_forms#new
edit_teacher_permission_form GET /teacher_permission_forms/:id/edit(.:format) teacher_permission_forms#edit
teacher_permission_form GET /teacher_permission_forms/:id(.:format) teacher_permission_forms#show
PUT /teacher_permission_forms/:id(.:format) teacher_permission_forms#update
DELETE /teacher_permission_forms/:id(.:format) teacher_permission_forms#destroy
trip_deposits GET /trip_deposits(.:format) trip_deposits#index
POST /trip_deposits(.:format) trip_deposits#create
new_trip_deposit GET /trip_deposits/new(.:format) trip_deposits#new
edit_trip_deposit GET /trip_deposits/:id/edit(.:format) trip_deposits#edit
trip_deposit GET /trip_deposits/:id(.:format) trip_deposits#show
PUT /trip_deposits/:id(.:format) trip_deposits#update
DELETE /trip_deposits/:id(.:format) trip_deposits#destroy
trip_fees GET /trip_fees(.:format) trip_fees#index
POST /trip_fees(.:format) trip_fees#create
new_trip_fee GET /trip_fees/new(.:format) trip_fees#new
edit_trip_fee GET /trip_fees/:id/edit(.:format) trip_fees#edit
trip_fee GET /trip_fees/:id(.:format) trip_fees#show
PUT /trip_fees/:id(.:format) trip_fees#update
DELETE /trip_fees/:id(.:format) trip_fees#destroy
team_dues GET /team_dues(.:format) team_dues#index
POST /team_dues(.:format) team_dues#create
new_team_due GET /team_dues/new(.:format) team_dues#new
edit_team_due GET /team_dues/:id/edit(.:format) team_dues#edit
team_due GET /team_dues/:id(.:format) team_dues#show
PUT /team_dues/:id(.:format) team_dues#update
DELETE /team_dues/:id(.:format) team_dues#destroy
如何让两条线路正常工作?
答案 0 :(得分:1)
respond_to do |format|
format.html { redirect_to(url_for(controller: 'students', action: 'show', id: student_id), notice: 'Safety test deleted successfully.') } # actually can be without url_for
format.js { render nothing: true }
end
第二个原因可能是HTML请求不包含DELETE方法,因此ActionDispatch认为您请求:action =&gt; &#39;秀&#39;在SafetyTestsController中。检查一下。
<%= link_to 'Delete', {controller: 'safety_tests', action: 'destroy', id: safety_test.id}, remote: true, method: :delete, :data => { confirm: 'Are you sure?' }, id: "delete_safety_test" %>
为什么不使用命名路线?:
<%= link_to 'Delete', safety_test, remote: true, method: :delete, :data => { confirm: 'Are you sure?' }, id: "delete_safety_test" %>
答案 1 :(得分:0)
找到答案。这是适用于JS和HTML的代码:
<%= form_for(safety_test, method: :delete, remote: true, html: {class: "delete_safety_test"}) do |f| %>
<%= f.submit "Delete", confirm: "Are you sure?" %>
<% end %>