繁体   English   中英

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

[英]Ruby on Rails- Destroy record using link

我想使用表数据旁边的链接删除存储在表中的记录。 我想出的错误是:

没有路线匹配[GET]“ / carlogs / destroy”

我的销毁方法:

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

redirect_to show_path(@carlog)
end

视图代码中包含“删除”链接的部分:

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

确保您具有以下delete REST方法:

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

application.js您必须有以下行:

//= require jquery_ujs

使用销毁链接销毁记录,然后重定向表,例如

方法

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

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

routes.rb

 delete 'destroy' => 'carlogs#destroy'

视图

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

我想会帮你的

你为什么写show_path(car)?

也许您是说car_path(car)?

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

另外,您还应该检查您的路线[GET] "/carlogs/destroy 。我认为这在rake router不存在

暂无
暂无

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

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