如何干我看起来像这样的路线
post 'products/sum', to: 'product#sum'
post 'products/subtract', to: 'product#subtract'
post 'cars/sum', to: 'car#sum'
post 'cars/subtract', to: 'car#subtract'
post 'houses/sum', to: 'house#sum'
post 'houses/subtract', to: 'house#subtract'
类似
post '%w{products,cars,houses}/%{sum subtract}', to: '%w{products,cars,houses}#%{sum subtract}'
我是否只想知道以上路线是否属于DRYable,如果是,可以如何做到这一点。
答案 0 :(得分:0)
只需一个选项
%i(products cars houses).each do |item|
resource item, only: [] do
post :sum
post :subtract
end
end
这会产生
➜ rake routes | grep -E "(house|car|product)"
sum_products POST /products/sum(.:format) products#sum
subtract_products POST /products/subtract(.:format) products#subtract
sum_cars POST /cars/sum(.:format) cars#sum
subtract_cars POST /cars/subtract(.:format) cars#subtract
sum_houses POST /houses/sum(.:format) houses#sum
subtract_houses POST /houses/subtract(.:format) houses#subtract