我试图通过Michael Hartl的ROR教程来追踪一个特别难以捉摸的错误。

单击微删除(从主页或用户/显示页面)的“删除”时,URL为http://localhost:3000/microposts/303,但结果为“路由错误-没有路由匹配” / microposts / 303。

我浏览了涉及到的代码的每一页,并用Hartl的gitHub项目站点中的代码替换了它们。 https://github.com/railstutorial/sample_app。例如,对于microposts_controller,我从gitHub软件仓库复制了代码,然后用复制的代码替换了我的代码。然后,我重新启动了服务器。结果相同。然后,我返回到我的代码以测试下一页。

我交换代码的页面是

控制器
microposts_controller
users_controller(显示方法)

模型
micropost.rb(模型)

意见
microposts / _micropost.haml
shared / _micropost_form.html.haml
共享的/_feed.haml
共享的/_feed_item.haml

和路线文件。

我不知所措要检查其他事情。有没有人有什么建议?

谢谢,

戴夫

耙路的结果

 sessions POST   /sessions(.:format)       {:action=>"create", :controller=>"sessions"}
new_session GET    /sessions/new(.:format)   {:action=>"new", :controller=>"sessions"}
    session DELETE /sessions/:id(.:format)   {:action=>"destroy", :controller=>"sessions"}
     signin        /signin(.:format)         {:controller=>"sessions", :action=>"new"}
    signout        /signout(.:format)        {:controller=>"sessions", :action=>"destroy"}
 microposts POST   /microposts(.:format)     {:action=>"create", :controller=>"microposts"}
  micropost DELETE /microposts/:id(.:format) {:action=>"destroy", :controller=>"microposts"}
       root        /(.:format)               {:controller=>"pages", :action=>"home"}
    contact        /contact(.:format)        {:controller=>"pages", :action=>"contact"}
      about        /about(.:format)          {:controller=>"pages", :action=>"about"}
       help        /help(.:format)           {:controller=>"pages", :action=>"help"}
     signup        /signup(.:format)         {:controller=>"users", :action=>"new"}
development        /development(.:format)    {:controller=>"pages", :action=>"development"}
                   /signup(.:format)         {:controller=>"users", :action=>"new"}
      users GET    /users(.:format)          {:action=>"index", :controller=>"users"}
            POST   /users(.:format)          {:action=>"create", :controller=>"users"}
   new_user GET    /users/new(.:format)      {:action=>"new", :controller=>"users"}
  edit_user GET    /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
       user GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}
            PUT    /users/:id(.:format)      {:action=>"update", :controller=>"users"}
            DELETE /users/:id(.:format)      {:action=>"destroy", :controller=>"users"}

Routes.rb文件是
SampleApp::Application.routes.draw do

#Sign in Routes
  resources :sessions, :only => [:new, :create, :destroy]
  match '/signin', :to => 'sessions#new'
  match '/signout', :to => 'sessions#destroy'

#Microposts Routes
  resources :microposts, :only => [:create, :destroy]


#Pages Routes
  root :to => "pages#home"

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'
  match '/signup',  :to => 'users#new'
  match '/development', :to => 'pages#development'

#Users Routes
  match '/signup',  :to => 'users#new'
  resources :users

end

但是,正如我所说,即使用gitHub上的路由文件替换了我的路由文件也无法解决问题。

要删除的链接是
 = link_to "delete", micropost, :method => :delete,
                                      :confirm => "You sure?",
                                      :title => micropost.content

最佳答案

link_to :method => :delete使用简洁的javascript创建DELETE请求。我的猜测是您要么项目中没有必需的javascript文件(prototype.js / jquery.js和rails.js),要么不将它们包含在布局中。

10-08 04:21