2010-08-15 75 views
0

我有考试控制器。RoR路由非常基本问题

在routes.rb中有“资源:考试”

在控制器有REST样生成的方法。

我想加上我自己的方法有:

def search 
    @exams = Exam.where("name like ?", params[:q]) 
end 

鉴于文件:

<%= form_tag(search_path, :method => "post") do %> 
    <%= label_tag(:q, "Szukaj: ") %> 
    <%= text_field_tag(:q) %> 
    <%= submit_tag("Szukaj") %> 
<% end %> 

我所知,尚无结果演示,它不会在这在所有的工作瞬间(:

当我去http://localhost:3000/exams/search它映射它显示控制器和搜索是:ID参数然后...

如何让http://localhost:3000/exams/search运行搜索控制器?

回答

3

你忘了添加路线。把这个routes.rb,前resources :exams

map.search '/exams/search', :controller => :exams, :action => :search 

注意,这resources :exams不会产生对控制器的所有公共方法的路线,它会产生非常具体的路线。您可以在rails routing guide中找到更多信息。 (特别参见3.2节)

+0

澄清溶液,谢谢 – matiit 2010-08-15 20:48:10

2

您需要为映射添加其他参数。您可以添加“收藏”的方法,像这样:

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

当你rake routes,你会看到,它产生的东西,像这样:

search_exams GET /exams/search(.:format) {:controller=>"exams", :action=>"search"} 
     exams GET /exams(.:format)   {:controller=>"exams", :action=>"index"} 
      POST /exams(.:format)   {:controller=>"exams", :action=>"create"} 
    new_exam GET /exams/new(.:format)  {:controller=>"exams", :action=>"new"} 
    edit_exam GET /exams/:id/edit(.:format) {:controller=>"exams", :action=>"edit"} 
     exam GET /exams/:id(.:format)  {:controller=>"exams", :action=>"show"} 
      PUT /exams/:id(.:format)  {:controller=>"exams", :action=>"update"} 
      DELETE /exams/:id(.:format)  {:controller=>"exams", :action=>"destroy"}