Search code examples
ruby-on-railscontrollerrouteslink-to

Creating a delete route for page controller


I have an application that creates page templates. There are many pages (user templates) in a User's account. I am getting the following error when I click my delete button:

browser:

Routing Error

No route matches [DELETE] "/pages/1"

html:

 <%= link_to 'Delete', page_path(page), :method => :delete, 
                                        :confirm => "Are you sure you want to delete?", 
                                        :remote => true, 
                                        class: 'btn btn-table-action btn-danger' %> 

controller:

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

  render :index
end

routes:

    pages GET    /pages(.:format)                                 pages#index
          POST   /pages(.:format)                                 pages#create
edit_page GET    /pages/:id/edit(.:format)                        pages#edit
     page GET    /pages/:id(.:format)                             pages#show
          PUT    /pages/:id(.:format)                             pages#update

UPDATE:

routes.rb:

resources :pages, only: [:index, :show, :create, :edit, :update] do
  resource :optin_integration, only: [:edit, :update]
end 

Where do I begin to create a delete route and make my delete button functional in Rails?


Solution

  • You need to make the pages resource available on destroy.

    resources :pages, only: [:index, :show, :create, :edit, :update, :destroy] do
      resource :optin_integration, only: [:edit, :update]
    end 
    

    Here is a nice tutorial on Rails routing.