2011-01-20 5 views
2

私は、彼らが私のルートを簡略化/再構築する(2)!

images/list_filtered?order=<order>&page=<page>&per_page=<count_per_page>&user_id=<user_id>&device_id=<device_id> 

2/USER_IDのユーザーという人の画像を取得するパラメータとして送信フィルタに従って対応

1 /取得画像と、ここで、画像の私のリストを取得する方法を持っています以下の通りです:ニュースフィード(マップの内側すなわち)

images/news_feed?order=<order>&page=<page>&per_page=<count_per_page>&user_id=<user_id>&device_id=<device_id> 

境界の内側に3 /画像

images/inside?order=<order>&page=<page>&per_page=<count_per_page>&user_id=<user_id>&device_id=<device_id>&lat1=<lat1>&lng1=<lng1>&lat2=<lat2>&lng2=<lng2> 

しかし、画像は資源である(そしてlist_filtered、news_feed、または内部IDとみなされるであろう)場合、我々はroutes.rbをして、このようにそれを定義することはできません

だから私は2つのソリューションを参照してください。

1 /これらのためのRESTのアプローチを破る画像リソース外3つのカスタムルート、:内部

images_list/filtered 
images_list/news_feed 
images_list/inside 

2 /濾過し、news_feedともパラメータを取得している、と私はself.send(params[:type])

ようなもので indexアクション内派遣します

両方のソリューションはかなり醜いですし、正しいアプローチ、考えを見つけることを望みますか?

答えて

3

したいと仮定すると、あまりにもすべての通常のリソースルート:

resources :images do 
    collection do 
    get 'filtered' 
    get 'news_feed' 
    get 'inside' 
    end 
end 

その後、rake routes意志出力:

filtered_images GET /images/filtered(.:format) {:action=>"filtered", :controller=>"images"} 
news_feed_images GET /images/news_feed(.:format) {:action=>"news_feed", :controller=>"images"} 
    inside_images GET /images/inside(.:format) {:action=>"inside", :controller=>"images"} 
      images GET /images(.:format)   {:action=>"index", :controller=>"images"} 
       POST /images(.:format)   {:action=>"create", :controller=>"images"} 
     new_image GET /images/new(.:format)  {:action=>"new", :controller=>"images"} 
     edit_image GET /images/:id/edit(.:format) {:action=>"edit", :controller=>"images"} 
      image GET /images/:id(.:format)  {:action=>"show", :controller=>"images"} 
       PUT /images/:id(.:format)  {:action=>"update", :controller=>"images"} 
       DELETE /images/:id(.:format)  {:action=>"destroy", :controller=>"images"} 

http://guides.rubyonrails.org/routing.html#adding-collection-routes

関連する問題