简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM