为什么我在"删除"请求中收到"无路由匹配[GET]"?Ruby on Rails

Las*_*ser 2 routes get ruby-on-rails http-delete

当我尝试删除communityfeed_item/micropost的内容时,我收到以下错误:

Routing Error

No route matches [GET] "/microposts/45"
Try running rake routes for more information on available routes.
Run Code Online (Sandbox Code Playgroud)

我检查我的路线并看到删除和发布,这是我真正需要的功能,但我一直收到"获取"错误.

posts GET    /posts(.:format)               posts#index
               POST   /posts(.:format)               posts#create
      new_post GET    /posts/new(.:format)           posts#new
     edit_post GET    /posts/:id/edit(.:format)      posts#edit
          post GET    /posts/:id(.:format)           posts#show
               PUT    /posts/:id(.:format)           posts#update
               DELETE /posts/:id(.:format)           posts#destroy
     locations GET    /locations(.:format)           locations#index
               POST   /locations(.:format)           locations#create
  new_location GET    /locations/new(.:format)       locations#new
 edit_location GET    /locations/:id/edit(.:format)  locations#edit
      location GET    /locations/:id(.:format)       locations#show
               PUT    /locations/:id(.:format)       locations#update
               DELETE /locations/:id(.:format)       locations#destroy
    communitys GET    /communitys(.:format)          communitys#index
               POST   /communitys(.:format)          communitys#create
 new_community GET    /communitys/new(.:format)      communitys#new
edit_community GET    /communitys/:id/edit(.:format) communitys#edit
     community GET    /communitys/:id(.:format)      communitys#show
               PUT    /communitys/:id(.:format)      communitys#update
               DELETE /communitys/:id(.:format)      communitys#destroy
         users GET    /users(.:format)               users#index
               POST   /users(.:format)               users#create
      new_user GET    /users/new(.:format)           users#new
     edit_user GET    /users/:id/edit(.:format)      users#edit
          user GET    /users/:id(.:format)           users#show
               PUT    /users/:id(.:format)           users#update
               DELETE /users/:id(.:format)           users#destroy
      sessions POST   /sessions(.:format)            sessions#create
   new_session GET    /sessions/new(.:format)        sessions#new
       session DELETE /sessions/:id(.:format)        sessions#destroy
    microposts POST   /microposts(.:format)          microposts#create
     micropost DELETE /microposts/:id(.:format)      microposts#destroy
                      /communitys(.:format)          communitys#index
                      /users(.:format)               users#index
  profile_info        /profile/info(.:format)        users#info
          join        /join(.:format)                users#new
         login        /login(.:format)               sessions#new
        logout DELETE /logout(.:format)              sessions#destroy
          root        /                              static_pages#home
          help        /help(.:format)                static_pages#help
         about        /about(.:format)               static_pages#about
       contact        /contact(.:format)             static_pages#contact
                      /posts(.:format)               static_pages#posts
       meetups        /meetups(.:format)             static_pages#meetups
          chat        /chat(.:format)                static_pages#chat
        groups        /groups(.:format)              static_pages#groups
           web        /web(.:format)                 static_pages#web
       profile        /profile(.:format)             static_pages#profile
Run Code Online (Sandbox Code Playgroud)

我在shared/_communityfeed_item.html.erb中的视图代码包含一个删除方法:

<% if current_user?(communityfeed_item.user) %>
        <%= link_to "delete", communityfeed_item, method:  :delete,
                                         confirm: "You sure?",
                                         title:   communityfeed_item.content %>
      <% end %>
Run Code Online (Sandbox Code Playgroud)

然而,当我点击删除时,由于某种原因,我发送了一个"获取"请求,这会产生路由错误.有谁知道为什么会发生这种情况?

这很麻烦,即使在我使用此删除请求的个人资料页面上:

<% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method:  :delete,
                                     confirm: "You sure?",
                                     title:   micropost.content %>
    <% end %>
Run Code Online (Sandbox Code Playgroud)

我仍然得到无路线匹配[GET]!

甚至更奇怪,这是HTML渲染:

<a href=?"/?microposts/?48" title="heyheyhye ">?delete?</a>?
<a href=?"/?microposts/?47" title=?"ddsf">?delete?</a>?
Run Code Online (Sandbox Code Playgroud)

尽管这是嵌入式红宝石:

<% if current_user?(communityfeed_item.user) %>
        <%= link_to "delete", communityfeed_item, method:  :delete,
                                         confirm: "You sure?",
                                         title:   communityfeed_item.content %>
      <% end %>
Run Code Online (Sandbox Code Playgroud)

在routes.rb中:

Meetumea::Application.routes.draw do
  resources :comments

  resources :posts

  resources :locations

  resources :communitys
  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :microposts,  only: [:create, :destroy]

  match '/communitys', to: "communitys#index"

  match '/users', to: "users#index"
  match '/profile/info', to: "users#info"

  match '/join', to: "users#new"
  match '/login',  to: 'sessions#new'
  match '/logout', to: 'sessions#destroy', via: :delete

  root to: "static_pages#home"
  match '/help', to: "static_pages#help"
  match '/about', to: "static_pages#about"
  match '/contact', to: "static_pages#contact"
  match '/posts', to: "static_pages#posts"
  match '/meetups', to: "static_pages#meetups"
  match '/chat', to: "static_pages#chat"
  match '/groups', to: "static_pages#groups"
  match '/web', to: "static_pages#web"
  match '/profile', to: "static_pages#profile"
end
Run Code Online (Sandbox Code Playgroud)

Ben*_*ger 6

听起来您的页面中包含不引人注目的JavaScript存在问题.

在你的主布局(layouts/application.erb.html或其中),你看到了javascript_include_tag :application什么?那就是指向app/assets/javascripts/application.js.你应该在那个文件中看到这个.

//= require jquery
//= require jquery_ujs
Run Code Online (Sandbox Code Playgroud)

最后一行确保用于不引人注目的JavaScript的必要库正在加载页面请求.

然后在你的Gemfile,你应该有gem "jquery-rails".您需要确保最近添加它,从命令行bundle install在项目目录中运行以确保它已安装.

你有没有看到这样的东西?rails.js我认为你不会看到像装载的那样的东西,因为那将是旧的版本jquery-ujsjquery-rails.当您查看呈现页面的源时,data-method如果JavaScript库包含在页面上并正常运行,您应该在删除链接上看到一个属性.

我愿意打赌 - 美元到甜甜圈 - 这是一个JavaScript问题.Rails使用jquery_ujs脚本文件来确保"删除"链接使用DELETE HTTP谓词,这类似于POST操作,但仅在删除资源时使用.否则,链接最终将作为普通的旧GET操作(如果JavaScript未正确加载).jquery_ujs负责发射的东西如新的链接标签data-methoddata-confirm基于什么它是由雇员再培训局渲染给定的(有人可以纠正我,如果我错了是如何工作的,我很好可能).

根据您的链接标记呈现的内容,我会说一些不引人注目的JavaScript是不对的.这描述了应该发生的事情(具体来说,关于发送的部分:method => symbol).在这一点上,我不知道该告诉你什么; 你的JavaScript肯定会出现问题.它甚至加载?如果您使用的是Chrome或Safari,则可以使用Web Inspector中的"脚本"面板查看Rails Asset Pipeline是否正在引入必要的脚本(jquery.jsjquery_ujs.js); 如果您使用的是Firefox,那么Firebug就是您的最佳选择.除此之外......我很茫然.这里陈述的一切都应该适合你.