All threads / why destroy action is called two times.

Ask A Question

Notifications

You’re not receiving notifications from this thread.

why destroy action is called two times.

naveen kumar asked in Rails

my destroy action

def destroy
@owner = User.find(params[:id])
@owner.destroy

respond_to do |format|
format.html { redirect_to root_path }
format.js {}
end

my index page delete link

<%= link_to admins_manage_client_path(owner), method: :delete,id: owner.id, remote: true, class: "btn btn-danger" do %>

<% end%>

         my server logs

         Started DELETE "/admins/manage_clients/12" for 127.0.0.1 at 2017-03-12 10:55:37 +0530

Processing by Admins::ManageClientsController#destroy as JS
Parameters: {"id"=>"12"}
Geokit is using the domain: localhost
User Load (0.6ms) SELECT users.* FROM users WHERE users.id = 12 LIMIT 1
(0.1ms) BEGIN
SQL (0.2ms) DELETE FROM users_roles WHERE users_roles.user_id = 12
SQL (0.2ms) DELETE FROM users WHERE users.id = 12
Started DELETE "/admins/manage_clients/12" for 127.0.0.1 at 2017-03-12 10:55:37 +0530
Processing by Admins::ManageClientsController#destroy as JS
Parameters: {"id"=>"12"}
Geokit is using the domain: localhost
User Load (0.3ms) SELECT users.* FROM users WHERE users.id = 12 LIMIT 1
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)

ActiveRecord::RecordNotFound (Couldn't find User with 'id'=12):

app/controllers/admins/manage_clients_controller.rb:24:in `destroy'
(56.1ms) COMMIT
Rendering admins/manage_clients/destroy.js.erb
Rendered admins/manage_clients/destroy.js.erb (0.4ms)
Completed 200 OK in 65ms (Views: 2.8ms | ActiveRecord: 57.3ms)

Rendering /home/nav/.rvm/gems/[email protected]/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
Rendering /home/nav/.rvm/gems/[email protected]/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.text.erb
Rendered /home/nav/.rvm/gems/[email protected]/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (0.7ms)
Rendering /home/nav/.rvm/gems/[email protected]/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
Rendered /home/nav/.rvm/gems/[email protected]/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.7ms)
Rendering /home/nav/.rvm/gems/[email protected]/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
Rendered /home/nav/.rvm/gems/[email protected]/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.1ms)
Rendered /home/nav/.rvm/gems/[email protected]/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (15.3ms)

Faced the same isssue, culprit was Turbolinks..be careful with the naming of your "ready" functions and also try not to use the same name everywhere. All you need not use is "turbolinks:load" mostly.

this is my application.js
//
//= require jquery2
//= require jquery_ujs
// require turbolinks
// require_tree .

already disabled turbolinks

Hi naveen,

Could you post your User and UserRole models, mainly the associations you have for them? Also, if you have any before / after actions being called, post those as well. I'm wondering if you have an unnecessary depenedent: destroy or after_destroy in there.

Also, do you have any special function through Geokit that maybe looking to delete something with that user model? Maybe a before/after action? It looks like something is trying to re-query for a User with an id of 12 (which was just deleted) to do something else... if so, whatever that action is either needs to be removed if it's redundant, or moved higher up the chain before the final User object is destroyed.

Geokit is using the domain: localhost
User Load (0.3ms) SELECT users.* FROM users WHERE users.id = 12 LIMIT 1
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)

ActiveRecord::RecordNotFound (Couldn't find User with 'id'=12):

<%= link_to "YES", admins_manage_client_path(owner),remote: true, method: :delete,id: owner.id, class: "btn btn-danger", :'data-dismiss' =>"modal" %>

now its working  without any double calling destroy

What really was the fix here, was it closing the modal when the action is called? Was the modal trying to update after the destroy?

when i put this link_to in the modal it automatically started working correctly . You are right modal trying to update after the destroy , the code is working fine now.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,909+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.