简体   繁体   English

Ruby on Rails-使用链接销毁记录

[英]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" 没有路线匹配[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 REST方法:

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

And in your application.js you must have this line: application.js您必须有以下行:

//= 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 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)? 你为什么写show_path(car)?

Maybe you mean car_path(car) ? 也许您是说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 另外,您还应该检查您的路线[GET] "/carlogs/destroy 。我认为这在rake router不存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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