[英]ruby-on-rails - custom method and link_to
I have a program with users and projects (many to many relation). 我有一个包含用户和项目的程序(很多关系)。 I would like to create my own methods: to delete all projects from specific user and to delete specific project from specific user, but I can't handle that.
我想创建自己的方法:从特定用户删除所有项目,并从特定用户删除特定项目,但是我无法处理。 There is (quiet big) possibility I don't understand routes.
我不了解路线的可能性很大(很大)。
Below I insert code to delete all project from specific user. 在下面插入代码以删除特定用户的所有项目。
In my user_controller.rb I have: 在我的user_controller.rb中,我有:
def delete_projects_from_user
@user.projects.delete_all
end
In show.html.erb link_to: 在show.html.erb link_to中:
<%= link_to 'Delete all projects', @user, method: :delete_projects_from_user, data: { confirm: 'Are you sure?' } %>
And in routes I tried among others this two option: 在路线中,我尝试了以下两个选择:
resources :users do
get 'delete_projects_from_user', on: :member
end
or 要么
post '/users/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
First option trows: "No route matches [POST] "/users/(id)" Second option just do nothing. 第一个选项抛出:“没有路由与[POST]相匹配/用户/(id)”第二个选项什么也不做。
I will be grateful for prompt. 我将不胜感激。
For this 为了这
def delete_projects_from_user
@user.projects.delete_all
end
You better user .destroy_all to make sure this object and all of it's associated items are destroyed as well 您最好使用.destroy_all来确保该对象及其所有关联项也被销毁
.delete_all only delete the object and it leaves the associated entries on the DB .delete_all仅删除对象,并将相关条目保留在数据库中
and As for this: 至于:
<%= link_to 'Delete all projects', @user, method: :delete_projects_from_user, data: { confirm: 'Are you sure?' } %>
In your route you defined your route as post so it should be 在您的路线中,您将路线定义为发布,因此应该
method: :post
to be like this 像这样
<%= link_to 'Delete all projects', @user, method: :post, data: { confirm: 'Are you sure?' } %>
And here you haven't added the route correctly, it should be like this 而且这里您没有正确添加路线,应该像这样
<%= link_to 'Delete all projects', YOUR_ROUTE_path(@user), method: :post, data: { confirm: 'Are you sure?' } %>
While it's recommended to define this route like this 虽然建议像这样定义此路线
delete '/users/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
As for the 2nd option, you can use collection as well 至于第二个选项,您也可以使用collection
resources :users do
collection do
delete 'user/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
end
end
and modify the link to be 并将链接修改为
<%= link_to 'Delete all projects', delete_projects_from_user(@user), method: :delete, data: { confirm: 'Are you sure?' } %>
Both options are fine, and the 2nd one with delete is the recommended one 这两个选项都很好,建议使用第二个删除选项
Basic format: 基本格式:
<%= link_to 'DISPLAY TEXT', YOUR_ROUTE_path(@object), method:
:HTTP_METHOD, data: { Additional html params } %>
Here is the solution: 解决方法如下:
<%= link_to 'Remove All Projects', delete_projects_from_user_path(@user), method: :post, data: { confirm: 'Are you sure?' } %>
Then in your method: 然后在您的方法中:
def delete_projects_from_user
user = user.find(params[:id])
user.projects.delete_all
redirect_to :back #if nothing to render
end
I'm sure this may help you. 我相信这可能对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.