简体   繁体   中英

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:

def delete_projects_from_user
    @user.projects.delete_all
end

In 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.

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

.delete_all only delete the object and it leaves the associated entries on the DB

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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