【问题标题】:Creating a delete route for page controller为页面控制器创建删除路由
【发布时间】:2013-07-16 21:06:28
【问题描述】:

我有一个创建页面模板的应用程序。用户帐户中有许多页面(用户模板)。单击删除按钮时出现以下错误:

浏览器

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' %> 

控制器

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

  render :index
end

路线

    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

更新

routes.rb

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

我应该从哪里开始创建删除路由并使我的删除按钮在 Rails 中起作用?

【问题讨论】:

  • 你能发布你的config/routes.rb文件是什么样子的吗?
  • 我在 routes.rb 文件中包含了:pages
  • 是的,然后按照@screenmutt 的建议进行操作,并将 :destroy 添加到only
  • 我喜欢 stackoverflow。我现在遇到了一个不同的错误,但这成功了。谢谢@liamneesonsarmsauce

标签: ruby-on-rails controller routes link-to


【解决方案1】:

您需要使页面资源在销毁时可用。

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

这是一个不错的tutorial on Rails routing

【讨论】:

    【解决方案2】:

    看起来您需要在config/routes.rb 文件中添加

    resources :pages
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-08
      • 2013-09-26
      • 1970-01-01
      • 2012-05-14
      • 2018-11-19
      • 2020-02-29
      • 1970-01-01
      • 2012-12-20
      相关资源
      最近更新 更多