Kevin
Kevin

Reputation: 171

Ruby on Rails- Destroy record using link

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 %>

Upvotes: 1

Views: 811

Answers (3)

inthedark122
inthedark122

Reputation: 104

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

Upvotes: 0

fool-dev
fool-dev

Reputation: 7777

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

Upvotes: 0

Khanh Pham
Khanh Pham

Reputation: 2973

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

Upvotes: 1

Related Questions