0

アプリケーションでアクションへの AJAX 呼び出しを行いたいのですが、ガイドAjax in Rails 3.1 - A Roadmapdestroyの例 #3 に従っています。Rails 3.2.8 を使用しています。

AJAX リクエストが正常に機能し、destroy期待どおりにアクションが呼び出されました。ユーザーがアクションの HTML ページを要求したときにdestroy、アイテムを削除してから別のページにリダイレクトしたいと考えています。

問題は、リダイレクトするコードを挿入すると、HTML と AJAX の両方のリクエストが機能しなくなることです。コードがある場合、AJAX リクエストは 500 Internal Server Error を返し、HTML リクエストは「The action 'show' could not be found for SafetyTestsController」を返します。2 つの行が存在しない場合、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>

app/controllers/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

2行を機能させるにはどうすればよいですか?

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

2 つ目の理由は、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 に答える