在 Hartl 的 ruby-on-rails 教程的第 11 章中删除 Micropost 时出现“没有路由匹配"错误 - 完全被难住了 [英] 'No route matches' Error on Delete of Micropost in Chapter 11 of Hartl's ruby-on-rails tutorial - completely stumped
问题描述
我正在尝试通过 Michael Hartl 的 ROR 教程找出一个特别难以捉摸的错误.
I am trying to track down a particularly elusive bug in working through Michael Hartl's ROR Tutorial.
当点击微博的删除"(从主页或用户/显示页面)时,URL 是 http://localhost:3000/microposts/303,但结果是路由错误 - 没有路由匹配"/microposts/303.
When clicking on 'Delete' for a micropost (from the home page or the user/show page) the url is http://localhost:3000/microposts/303, but the result is "Routing Error - No route matches"/microposts/303".
我浏览了涉及的代码的每一页,并将它们替换为来自 Hartl 的 gitHub 项目站点的代码.https://github.com/railstutorial/sample_app.例如,对于 microposts_controller,我从 gitHub 仓库复制了代码,并用复制的代码替换了我的代码.然后我重新启动了服务器.结果相同.然后我回到我的代码来测试下一页.
I have gone through each page of my code that is involved and replaced them with code from Hartl's gitHub project site. https://github.com/railstutorial/sample_app. For example, for the microposts_controller, I copied the code from the gitHub depot and replaced my code with the copied code. I then restarted the server. Same result. I then reverted back to my code to test the next page.
我交换代码的页面是
控制器microposts_controllerusers_controller(显示方法)
CONTROLLERS microposts_controller users_controller (show method)
型号micropost.rb (模型)
MODEL micropost.rb (model)
视图microposts/_micropost.haml共享/_micropost_form.html.haml共享/_feed.hamlshared/_feed_item.haml
VIEWS microposts/_micropost.haml shared/_micropost_form.html.haml shared/_feed.haml shared/_feed_item.haml
还有 Routes 文件.
and the Routes file.
我不知道要检查其他事情.有人有什么建议吗?
I am at a loss for other things to check. Does anyone have any suggestions?
谢谢,
戴夫
rake 路由的结果
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 文件是
The Routes.rb file is
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 上的路由文件替换我的路由文件也不能解决问题.
But, as I said, even replacing my routes file with the one on gitHub did not resolve the issue.
要删除的链接是
= 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),或者您的布局中没有包含它们.
link_to :method => :delete
uses unobtrusive javascript to create the DELETE
request. My guess is that you either don't have the necessary javascript files in your project (prototype.js/jquery.js and rails.js) or you are not including them in your layout.
这篇关于在 Hartl 的 ruby-on-rails 教程的第 11 章中删除 Micropost 时出现“没有路由匹配"错误 - 完全被难住了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!