使用 Rails 路由优化 Axios POST 请求
使用 Rails 路由优化 Axios POST 请求。原本使用 axios.post('{{ route('products.favor', ['product' => $product->id]) }}') 的方式可以改成更简洁的 axios.post('/products/{{ @product.id }}/favor')。这可以通过在 Rails 中定义路由来实现,例如:
Rails.application.routes.draw do
resources :products do
post :favor, on: :member
end
end
这样,当访问 /products/{{ @product.id }}/favor 路径时,就会调用 ProductsController#favor 控制器方法,并传递 @product.id 作为参数。
通过使用 Rails 路由,可以使代码更简洁易读,同时也能提高代码的可维护性。
原文地址: https://www.cveoy.top/t/topic/pXIl 著作权归作者所有。请勿转载和采集!