我有办法获取我的图像列表,这里有相应的
1 /根据作为参数发送的过滤器获取图像
images/list_filtered?order=<order>&page=<page>&per_page=<count_per_page>&user_id=<user_id>&device_id=<device_id>
2 /获取user_id用户所关注的人的图片:新闻提要
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>
但是如果images是资源(然后list_filtered,news_feed或者inside会被视为ID),我们就无法在routes.rb中定义它。
所以我看到了两个解决方案:
图像资源之外的1/3自定义路由,打破了这些的REST方法:
images_list/filtered
images_list/news_feed
images_list/inside
2 / filtered,news_feed和inside也是获取参数,我在index
行动中发送类似self.send(params[:type])
这两种解决方案都非常难看,并希望找到正确的方法,任何想法?
答案 0 :(得分: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