1

I want to delete a record that is stored in a table using a link right next to the table data. The error I come up with is:

No route matches [GET] "/carlogs/destroy"

My destroy method:

def destroy
@carlog= CarLog.find(params[:id])
@carlog.destroy()

redirect_to show_path(@carlog)
end

Part of the view code containing the Delete link:

<% @carlogs.each do |car| %>
<tr>
<td><%= car.id %></td>
<td><%= car.plate_number %></td>
<td><%= car.brand %></td>
<td><%= car.slot_number %></td>
<td><%= car.is_taken %></td>
<td><%= car.created_at %></td>
<td><%= link_to "Delete", show_path(car), method: :delete, data: 
        {confirm: "Are you sure?"} %>
</tr>
<% end %>
1
  • run rake routes to see the correct path for deleting object. Commented Jul 5, 2016 at 10:47

3 Answers 3

1

Make sure that you have delete REST method as:

DELETE /carlogs/:id(.:format)                     carlogs#destroy

And in your application.js you must have this line:

//= require jquery_ujs
Sign up to request clarification or add additional context in comments.

Comments

0

Use destroy link for destroy record then redirect table like

method

def destroy
 @carlog= CarLog.find(params[:id])
 @carlog.destroy()

 redirect_to show_path(@carlog) #-> Table pathe
end

routes.rb

 delete 'destroy' => 'carlogs#destroy'

View

<%= link_to "Delete", destroy_path, method: :delete, data: 
    {confirm: "Are you sure?"} %>

I think will help you

Comments

0

Why did you write show_path(car)?

Maybe you mean car_path(car) ?

<%= link_to "Delete", car_path(car), method: :delete, data: {confirm: "Are you sure?"}%>

Also you should check your route [GET] "/carlogs/destroy. I think this don't present in rake router

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.