0

我想destroy在我的应用程序中对操作进行 AJAX 调用,并且正在遵循Rails 3.1 中的指南 Ajax 的示例 #3 -执行此操作的路线图。我正在使用 Rails 3.2.8。

我让 AJAX 请求工作得很好,并且destroy按照我的预期调用了该操作。当用户请求destroy操作的 HTML 页面时,我想删除该项目,然后将她重定向到另一个页面。

问题是当我插入代码进行重定向时,HTML 和 AJAX 请求都停止工作。当代码存在时,AJAX 请求会返回 500 Internal Server Error,而 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

我怎样才能让两条线工作?

4

2 回答 2

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 认为您在 SafetyTestsController 中请求 :action => 'show'。核实。

<%= 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" %>
于 2012-12-27T02:35:28.567 回答
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 %> 
于 2012-12-27T19:24:59.897 回答