【问题标题】:Rails - missing routes helper prefixRails - 缺少路由助手前缀
【发布时间】:2016-08-05 05:13:36
【问题描述】:

我缺少联系人#show 前缀“联系人”,如下所示。

搜索路线

               contacts GET    /contacts(.:format)                     contacts#index
                        POST   /contacts(.:format)                     contacts#create
            new_contact GET    /contacts/new(.:format)                 contacts#new
           edit_contact GET    /contacts/:id/edit(.:format)            contacts#edit
                        GET    /contacts/:id(.:format)                 contacts#show
                        PATCH  /contacts/:id(.:format)                 contacts#update
                        PUT    /contacts/:id(.:format)                 contacts#update
                        DELETE /contacts/:id(.:format)                 contacts#destroy

我想这就是为什么我在单击以下链接时得到一个点而不是斜线的原因。

_contact.html.erb

<%= link_to "delete contact", contact, method: :delete,
                                    data: { confirm: "You sure?" } %>

服务器记录正确的 DELETE 请求,但是无法呈现 /contact.26 而不是正确的 /contacts/26。

Started DELETE "/contact.26" for 128.177.12.30 at 2016-04-13 21:04:30 +0000

ActionController::RoutingError (No route matches [DELETE] "/contact.26"):

我遇到的每篇文章都用点而不是斜线似乎源于复数错误,但是,我不相信这里是这种情况。

此外,我已经从我的路由文件中删除了资源 :contacts,运行 $ rake 路由,添加了资源 :contacts,运行 $ rake 路由,问题仍然存在。

这个问题似乎是联系人模型独有的,因为我的其余模型在删除时没有丢失任何前缀或出现此错误。

如何将“联系人”前缀添加回“联系人#show”?

routes.rb 文件供参考:

Rails.application.routes.draw do
  root 'static_pages#home'
  get 'help' => 'static_pages#help'
  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup' => 'users#new'
  get 'login' => 'sessions#new'
  get 'newevent' => 'events#new'
  get 'newteam' => 'teams#new'
  get 'newperformance' => 'performances#new'
  get 'newhotel' => 'hotels#new'
  get 'newcontact' => 'contacts#new'
  get 'newflight' => 'flights#new'
  get 'newground' => 'grounds#new'
  get 'newguest' => 'guests#new'
  post 'login' => 'sessions#create'
  delete 'logout' => 'sessions#destroy'
  resources :users
  resources :events
  resources :teams do
    member do
      get :events
    end
  end
  resources :performances
  resources :hotels
  resources :contacts
  resources :flights
  resources :grounds
  resources :guests
  resources :account_activations, only: [:edit]
  resources :password_resets, only: [:new, :create, :edit, :update]
end

【问题讨论】:

  • 试试&lt;%= link_to "delete contact", contact_path(contact), method: :delete, data: { confirm: "You sure?" } %&gt;
  • 同样的路由错误,很遗憾。

标签: ruby-on-rails ruby routes


【解决方案1】:

您可能在

设置的路线之间存在冲突
 resources :contacts

和

get 'contact' => 'static_pages#contact'

最简单的解决方案可能是更改静态页面的链接。如果可以,最好将资源放在一起。

【讨论】:

    【解决方案2】:

    我刚刚遇到了同样的错误。

    我的问题的解决方法是资源末尾的's'

    我有“资源”,遇到了和你一样的问题。

    我将其更改为“资源”,它解决了我的问题

    在我的情况下只是一个错字。

    【讨论】:

      【解决方案3】:

      昨天我发现了这个错误。修复它的是删除未使用的帮助文件。可能值得你一试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-25
        • 1970-01-01
        相关资源
        最近更新 更多