开发者

Simplify/RESTify my routes(2)!

I have ways to get my list of images, here they with the corresponding

1/ get images according to filters sent as parameters

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

2/get the images of the people that the user_id's user is following: news feed

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

3/ images inside boundaries (i.e. inside a map)

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>

But we cannot define it like this in ro开发者_运维技巧utes.rb if images is a resource (then list_filtered, news_feed, or inside would be considered as an ID)

So I see 2 solutions:

1/ 3 custom routes outside the images resources, breaking the REST approach for these:

images_list/filtered
images_list/news_feed
images_list/inside

2/ filtered, news_feed and inside are also get parameters, and I dispatch inside the index action with something like self.send(params[:type])

Both solutions are pretty ugly, and would like to find the right approach, any thoughts?


Assuming you want all the normal resource routes too:

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

then rake routes will output:

 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜