Advanced Rails Recipes [问题] 高手进

Advanced Rails Recipes [问题] 高手进

书中21页,search那一段
访问 http://localhost:3000/events/search?q=rubyconf
错误提示
------------------------------------------------------------------------------------------------
ActiveRecord::RecordNotFound in EventsController#show
Couldn't find Event with ID=search
RAILS_ROOT: C:/rubydev/ad/REST

Application Trace | Framework Trace | Full Trace
D:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1379:in `find_one'
D:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1362:in `find_from_ids'
D:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:537:in `find'
app/controllers/events_controller.rb:16:in `show'
D:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1379:in `find_one'
D:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1362:in `find_from_ids'
D:/Program Files/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:537:in `find'
D:/Program Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1162:in `send'
D:/Program Files/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1162:in `perform_action_without_filters'
......................
.......................
............................
........................
Request
Parameters:

{"id"=>"search",
"q"=>"rubyconf"}

Show session dump

---
flash: !map:ActionController::Flash::FlashHash {}


Response
Headers:

{"cookie"=>[],
"Cache-Control"=>"no-cache"}
提示中说的很清楚了。他把你的'search'当成了params[:id]的值了。
我想问题可能是你的routes中没有添加search的route吧。看上面的信息你的events好像是用RESTFul的。
看书抄例子,要抄完整呀~~~
另外,建议下次的标题不要用“**高手进”,“重大发现***”等字样,呵呵~~题外话

routes中已经写了
------------------------------------------------------------------------------
 map.resources :events, :collection => { :search => :get }
但还是出错
是否还要写其他的东西

原本想把写的有错的程序上传的,但不知道为什么传不了.
你rake routes 看看是不有 “http://localhost:3000/events/search?q=rubyconf”的URL
好的,我试试..
如果有的话,把那条route的顺序往上调点,估计是在
route.map /:controller/:action/:id。。。啥的那条后面了吧。
C:\rubydev\ad\REST>rake routes
(in C:/rubydev/ad/REST)
    search_events GET  /events/search       {:controller=>"events", :action=>"search"}
formatted_search_events GET  /events/search.:format    {:controller=>"events", :action=>"search"}
      events GET  /events         {:controller=>"events", :action=>"index"}
   formatted_events GET  /events.:format      {:controller=>"events", :action=>"index"}
        POST /events         {:controller=>"events", :action=>"create"}
        POST /events.:format      {:controller=>"events", :action=>"create"}
     new_event GET  /events/new        {:controller=>"events", :action=>"new"}
  formatted_new_event GET  /events/new.:format     {:controller=>"events", :action=>"new"}
     edit_event GET  /events/:id/edit      {:controller=>"events", :action=>"edit"}
 formatted_edit_event GET  /events/:id/edit.:format   {:controller=>"events", :action=>"edit"}
      event GET  /events/:id        {:controller=>"events", :action=>"show"}
   formatted_event GET  /events/:id.:format     {:controller=>"events", :action=>"show"}
        PUT  /events/:id        {:controller=>"events", :action=>"update"}
        PUT  /events/:id.:format     {:controller=>"events", :action=>"update"}
        DELETE /events/:id        {:controller=>"events", :action=>"destroy"}
        DELETE /events/:id.:format     {:controller=>"events", :action=>"destroy"}
           /:controller/:action/:id
           /:controller/:action/:id.:format

rake 出来的...
有search的 action的.
这问题想了很久,就是不懂
route中
------------------------------
ActionController::Routing::Routes.draw do |map|

 map.resources :events, :collection => { :search => :get }
....................................

map我写在了第一句
看样子是没问题的呀。服务器重启过嘛?不先你把项目打包,发给我: qq:310515
在线等.... 这问题卡了2礼拜了.