从命名空间中删除

发布于 2024-12-27 11:54:58 字数 628 浏览 1 评论 0原文

我已经设置了一个管理命名空间,以便访问管理区域中的模型: /admin/pages

但是我有以下问题 例如,我无法让删除功能在 Admin::PageController 或我的任何模型下工作。

有谁知道该怎么做。 我有以下内容:

Admin::PageController 我有以下内容

def destroy
   @page = Page.find(params[:id])
   @page.destroy

   respond_to do |format|
     format.html { redirect_to admin_pages_url }
     format.json { head :ok }
   end
end

然后在我的页面索引文件上,我想要一个链接来删除记录,我有以下内容: (/admin/pages)

<%=link_to admin_page_path(page), :class => 'ico del' do %>
  <%='Delete'%>
<% end %>

似乎不起作用。有人知道如何让它发挥作用吗?

I have setup a admin namespace in order to access models in the admin area: /admin/pages

However i have the following problem
i cant get the delete function to work under Admin::PageController for example or any of my models.

Does anyone know how to do this.
I have the following:

Admin::PageController I have the following

def destroy
   @page = Page.find(params[:id])
   @page.destroy

   respond_to do |format|
     format.html { redirect_to admin_pages_url }
     format.json { head :ok }
   end
end

Then on my page index file where i want a link to delete the record i have the following: (/admin/pages)

<%=link_to admin_page_path(page), :class => 'ico del' do %>
  <%='Delete'%>
<% end %>

Does not seem to work. Anyone know how to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆之渊 2025-01-03 11:54:58

您错过了 link_to 调用中的 :method 选项

 link_to 'Delete', admin_page_path, :confirm => 'Are you sure?', :method => :delete 

<%=link_to admin_page_path(page), :class => 'ico del',:method => :delete do %>
  <%='Delete'%>
<% end %>

you have missed :method option in link_to call

 link_to 'Delete', admin_page_path, :confirm => 'Are you sure?', :method => :delete 

or

<%=link_to admin_page_path(page), :class => 'ico del',:method => :delete do %>
  <%='Delete'%>
<% end %>
贪恋 2025-01-03 11:54:58

link_to 帮助程序默认为 GET 请求,除非您指定其他属性来告诉它您希望如何处理它。

在这种情况下,您需要设置一些额外的参数:

<%=link_to "Delete", admin_page_path(page), :class => "ico del", :remote => true, :method => :delete %>

后台实际发生的是 Rails UJS (不显眼的 javascript 适配器)捕获点击事件并通过 AJAX 发送请求。因此,您应该看到它使用 POST 命中您的服务器(但它也传入 _method => delete)来删除该对象。

我还假设您的路线设置正确。像这样的东西:

namespace :admin do
  resources :pages
end

The link_to helper defaults to a GET request unless you specify additional attributes to tell it how you want it to be handled.

In this case, you need to set some extra arguments:

<%=link_to "Delete", admin_page_path(page), :class => "ico del", :remote => true, :method => :delete %>

What actually happens in the background is the Rails UJS (unobtrusive javascript adapter) captures the click event and sends the request via AJAX. So you should see it hit your server with a POST (but it passes in _method => delete as well) to delete the object.

I'm also assuming you have your routes set up correctly. Something like:

namespace :admin do
  resources :pages
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文