Logo

Rails Singular Resources

Rails 中 Controller 如果不是复数,写 path 或 url 的时候就必须加上 index,这样看上去就很 low。

resources :photo do  
  collection do    
    get 'search'  
  end
end

search_photo_index GET    /photo/search(.:format)

我们去查看 参考文档,发现可以这样做:

resource :photo, controller: photo do  
  collection do    
    get 'search'  
  end
end

search_photo GET    /photo/search(.:format)

需要注意的是经过这样修改后,访问 /photo 不再调用 index action 而是调用 show action。

comments powered by Disqus