1
votes

Having 2 models customer and comm_log. Their association is as below:

resources :customers do
  comm_logs
end

The rspec code for destroy in comm_logs controller passed without any error. The lambda block verified that the comm log count was reduced by 1 after a successful delete. Everything in controller seems right.

The output of rake routes is:

       new_customer_comm_log GET    /customers/:customer_id/comm_logs/new(.:format)                 {:action=>"new", :controller=>"comm_logs"}
      edit_customer_comm_log GET    /customers/:customer_id/comm_logs/:id/edit(.:format)            {:action=>"edit", :controller=>"comm_logs"}
           customer_comm_log GET    /customers/:customer_id/comm_logs/:id(.:format)                 {:action=>"show", :controller=>"comm_logs"}
                             PUT    /customers/:customer_id/comm_logs/:id(.:format)                 {:action=>"update", :controller=>"comm_logs"}
                             DELETE /customers/:customer_id/comm_logs/:id(.:format)                 {:action=>"destroy", :controller=>"comm_logs"}

In reality, the record was not deleted after clicking the delete button and the page was not redirected to previous page as it is in the controller (the show page just remains and did not redirect to anywhere after deleting). It seems that the delete action was routed to the right path. The problem is most likely with the link_to below in views:

<%= link_to 'delete', customer_comm_log_path(@customer, @comm_log), :method => :delete, :confirm => "are you sure?", :remote => true %>

Is there anything wrong in link_to above? thanks.

1
Can you post your controllers destroy method?andrewpthorp

1 Answers

1
votes

There are a couple things you may be missing. I'm assuming you're using jQuery since you didn't specify in your question.

a) Make sure you have jQuery installed

b) Make sure you have installed the Rails UJS (Unobtrusive Javascript) adapter either manually or using the gem (instructions for Rails 3.1 are on this link as well): https://github.com/rails/jquery-ujs

c) Your link_to needs to tell the Rails UJS adapter that it should capture the link's click and submit the DELETE request via AJAX

<%= link_to 'delete', customer_comm_log_path(@customer, @comm_log), :remote => true, :method => :delete) %>

So in the link_to above, you were missing :remote => true

Rails will capture the user's click on that link and then kick off a jQuery $.ajax call to your server that includes a couple params to tell the Rails server that it is a remote DELETE request.

d) If you're redirecting in the controller via the user clicking a remote link like that, you need to tell Rails to handle the AJAX redirects properly by placing this code in your ApplicationController. If you don't do this, the link will delete the record but then it will just sit there and not redirect.

  # Allows redirecting for AJAX calls as well as normal calls
  def redirect_to(options = {}, response_status = {})
    if request.xhr?
      render(:update) {|page| page.redirect_to(options)}
    else
      super(options, response_status)
    end
  end