2

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

Routing Error

No route matches [GET] "/microposts/45"
Try running rake routes for more information on available routes.

我检查我的路线并看到删除和发布,这是我真正需要的功能,但我一直收到“get”错误。

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

我在 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 %>

然而,当我单击删除时,出于某种原因,我正在发送一个“获取”请求,这会导致路由错误。有谁知道为什么会发生这种情况?

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

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

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

更奇怪的是,这是渲染的 HTML:

<a href=​"/​microposts/​48" title="heyheyhye ">​delete​&lt;/a>​
<a href=​"/​microposts/​47" title=​"ddsf">​delete​&lt;/a>​

等等尽管这是嵌入式红宝石:

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

在 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
4

3 回答 3

6

听起来您的页面中包含的不显眼的 JavaScript 存在问题。

在您的主布局中(在layouts/application.erb.html或其他地方),您在javascript_include_tag :application某处看到了吗?那将指向app/assets/javascripts/application.js. 您应该在该文件中看到这一点。

//= require jquery
//= require jquery_ujs

最后一行确保与页面请求一起加载不显眼的 JavaScript 所需的库。

那么在你的Gemfile,你应该有gem "jquery-rails"。您需要确保如果它是最近添加的,请从命令行bundle install在您的项目目录中运行以确保它已安装。

你看到类似的东西吗?我想你不会看到像rails.js加载这样的东西,因为旧版本的jquery-ujsor jquery-rails. 当您查看呈现页面的源代码时,data-method如果 JavaScript 库包含在页面中并且运行正常,您应该会在删除链接上看到一个属性。

我愿意打赌——美元对甜甜圈——这是一个 JavaScript 问题。Rails 使用jquery_ujs脚本文件来确保“删除”链接使用 DELETE HTTP 动词,这类似于 POST 操作,但仅在删除资源时使用。否则,链接最终会成为一个普通的旧 GET 操作(如果 JavaScript 没有正确加载)。jquery_ujs负责根据 ERB 渲染器给出的内容发出新的链接标签(如果我对它的工作原理有误,有人可以纠正我;我很可能是这样)data-methoddata-confirm

根据您的链接标签呈现的内容,我会说不显眼的 JavaScript 有什么不对劲的地方。这描述了应该发生的事情(特别是关于发送的部分:method => symbol)。在这一点上,我不知道该告诉你什么;你的 JavaScript 肯定有问题。它甚至加载吗?如果您使用的是 Chrome 或 Safari,则可以使用 Web Inspector 中的 Scripts 面板查看 Rails Asset Pipeline 是否正在拉入必要的脚本(jquery.jsjquery_ujs.js);如果您使用的是 Firefox,那么 Firebug 是您的最佳选择。除此之外……我不知所措。这里所说的一切都应该对你有用。

于 2012-04-26T01:08:10.630 回答
1

这是因为调用wrapin views/static_pages/home.html.erb

确实有效:

<%= render 'shared/communityfeed' %>

这不会:

<%= wrap render 'shared/communityfeed' %>

这是因为您sanitize在包装代码中使用:

它去除了所有未明确允许的属性。

于 2012-04-26T04:01:47.497 回答
0

因此,出于某种原因,意见中的<%= wrap render @Microposts %>原因<%= wrap render 'shared/communityfeed' %>是造成麻烦的原因。当我删除“包装”时,一切都开始正常工作。

包装代码:

def wrap(content)
  sanitize(raw(content.split.map{ |s| wrap_long_string(s) }.join(' ')))
end

def wrap_long_string(text, max_width = 50)
  zero_width_space = "&#8203;"
  regex = /.{1,#{max_width}}/
  (text.length < max_width) ? text : 
    text.scan(regex).join(zero_width_space)
end

请参阅:https ://stackoverflow.com/questions/10326180/why-is-rails-not-rendering-method-delete-into-html#comment13310733_10326180

于 2012-04-26T15:47:33.247 回答