Search code examples
ruby-on-rails-4rails-routing

button_to path routing in Rails


I'm having trouble routing a button so that it calls an action in a controller. To be more specific, I have this line

delete 'destroy/:id', to: 'users#admin_destroy' 

in my route file, and this line

%= button_to 'Destroy', destroy_path(user), data: { confirm: 'Are you sure?' }  %>

in a view.

The problem is that the page keeps treating destroy_path as a method, and I'm at a loss for what the proper syntax should be. What am I misunderstanding?

Additionally, I originally tried to get this work as link_to, but I learned that that should be reserved for GET requests. However, I don't like how button_to looks, and I'm wondering if there's a way to do this as a GET so that link_to can be used instead.


Solution

  • I'm not sure if this will completely solve your problem, but... You can custom define a path like so:

    in config/routes.rb

    delete 'destroy/:id', to: 'users#admin_destroy', as: :destroy
    

    This will allow you to use destroy_path in your application, and have it route to your users controller with the admin_destroy function.

    You can read more about it here