在我的应用程序的某个部分中,销毁操作对我有用,但我无法在使用单独控制器的其他视图中使用它。
我收到错误:No route matches {:action=>"destroy", :controller=>"letsgo"}
查看:
<% for letsgo in @letsgos %>
<li>
<b>Let's Go...<span class="content"><%= letsgo.content %></span></b>
<%= link_to 'Delete', { :controller => 'letsgo', :action => 'destroy'},
{ :confirm => 'Are you sure?', :method => :delete, :remote => true} %>
<% end %>
路线:
resources :letsgos, only: [:create, :destroy]
LetsGos控制器:
def destroy
@letsgo.destroy
redirect_to root_url
end
如果我在letsgos
视图下<%= link_to "delete", letsgo, method: :delete, data: { confirm: "You sure?" }%>
letsgos
视图下工作,销毁行动有效,但我在不同的文件夹下工作它不再有效。我正在做的是列出content
表中的所有letsgos
,并为每个内容提供销毁操作。
路线:
letsgos_eatdrink GET /letsgos/eatdrink(.:format) letsgos#eatdrink
letsgos_listenwatch GET /letsgos/listenwatch(.:format) letsgos#listenwatch
letsgos_play GET /letsgos/play(.:format) letsgos#play
letsgos_other GET /letsgos/other(.:format) letsgos#other
letsgos_explore GET /letsgos/explore(.:format) letsgos#explore
repost_letsgo POST /letsgos/:id/repost(.:format) letsgos#repost
interested_letsgo POST /letsgos/:id/interested(.:format) letsgos#interested
GET /letsgos(.:format) letsgos#index
POST /letsgos(.:format) letsgos#create
new_letsgo GET /letsgos/new(.:format) letsgos#new
edit_letsgo GET /letsgos/:id/edit(.:format) letsgos#edit
GET /letsgos/:id(.:format) letsgos#show
PATCH /letsgos/:id(.:format) letsgos#update
PUT /letsgos/:id(.:format) letsgos#update
DELETE /letsgos/:id(.:format) letsgos#destroy
答案 0 :(得分:1)
您没有将letsgo
的ID传递给路线:
<%= link_to 'Delete', { :controller => 'letsgos', :action => 'destroy', :id => letsgo.id },
{ :confirm => 'Are you sure?', :method => :delete, :remote => true} %>
正如你的路径所写:
letsgo DELETE /letsgos/:id(.:format) letsgos#destroy
它没有经过测试,但应该像这样
答案 1 :(得分:0)
这也适合我!只需在P
之前添加DATA:
...并确认删除对话框即可。
{ :confirm..... }