简体   繁体   English

不能被破坏在铁轨上工作

[英]Can't get destroy to work in rails

Fairly new to rails and can't seem to get this simple destroy action working. 对Rails来说还很陌生,似乎无法使这种简单的毁灭动作起作用。 All it does is redirect to the mod panel index page and doesn't destroy the record. 它所做的只是重定向到mod面板索引页面,并且不会破坏记录。 Do I need to call .destroy in the destroy method? 我需要在destroy方法中调用.destroy吗? or is there something I'm missing? 还是我想念的东西?

mod_approval controller mod_approval控制器

def index
    @guide          = Guide.friendly.find(params[:guide_id])
    @check_category = CheckCategory.where(guide_id: @guide.id).all
    @category       = Guide.friendly.find(@guide.id).categories.new
end

def destroy
    redirect_to guide_mod_panel_mod_approval_index_path(@guide)
end

config/routes.rb config / routes.rb

match '/guides/:guide_id/mod-panel/approve/reject' => 'mod_approval#destroy', :via => :delete, as: :guide_mod_panel_approve_destroy

index.html.erb index.html.erb

 <% @check_category.each do |category| %>
    <%= link_to "Reject", guide_mod_panel_approve_destroy_path(@guide, category), method: :delete, data: {confirm: "You sure?"} %><br>
 <% end %>

You need to fetch the record from the database, then you can call destroy on the object, you can do this 您需要从数据库中获取记录,然后可以在对象上调用destroy,您可以执行此操作

def destroy
  guide = Guide.find(params[:guide_id])
  category = guide.categories.find(params[:id])
  category.destroy
  redirect_to guide_mod_panel_mod_approval_index_path(guide) 
end

Hope that helps! 希望有帮助!

You should have to destroy object before: 您必须先销毁对象:

def destroy
 @destroyed_object.destroy # or Model.destroy(params[:id])
 redirect_to guide_mod_panel_mod_approval_index_path(@guide)
end

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

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