Rails 4 - 单例资源下的嵌套资源
Rails 4 - nested resource under singleton resource
我有一个 Rails 4 应用程序,目前正在实施购物车。
我不想透露用户 ID、购物车 ID,但只想透露 url 中的购物车商品 ID。
所以基本上,我希望我的路线是这样的;-
/user/cart/plans => show all plans
/user/cart/cart_items/new => add to cart
/user/cart => show current cart with cart items
/user/cart/cart_item DESTROY - delete item from cart
所以在我的 routes.rb
resource :user, only: [:edit] do
collection do
patch 'update_password'
get 'show_payment_history'
end
resource :cart
resources :cart_items , only: [:new, :destroy]
...
...
使用上述路由生成的路径是...
update_password_user PATCH /user/update_password(.:format) users#update_password
show_payment_history_user GET /user/show_payment_history(.:format) users#show_payment_history
user_cart POST /user/cart(.:format) carts#create
new_user_cart GET /user/cart/new(.:format) carts#new
edit_user_cart GET /user/cart/edit(.:format) carts#edit
GET /user/cart(.:format) carts#show
PATCH /user/cart(.:format) carts#update
PUT /user/cart(.:format) carts#update
DELETE /user/cart(.:format) carts#destroy
new_user_cart_item GET /user/cart_items/new(.:format) cart_items#new
user_cart_item DELETE /user/cart_items/:id(.:format) cart_items#destroy
但我仍然无法使其工作,例如,user_cart_path
无法在重定向中工作,不知道...这是什么问题。
对于我建议的解决方案的任何帮助或任何好的方法,我们将不胜感激!
提前致谢
问题与控制器多元化有关name.Once 我将控制器从 cart
重命名为 carts
...一切都已修复。
希望对别人有帮助
我有一个 Rails 4 应用程序,目前正在实施购物车。 我不想透露用户 ID、购物车 ID,但只想透露 url 中的购物车商品 ID。 所以基本上,我希望我的路线是这样的;-
/user/cart/plans => show all plans
/user/cart/cart_items/new => add to cart
/user/cart => show current cart with cart items
/user/cart/cart_item DESTROY - delete item from cart
所以在我的 routes.rb
resource :user, only: [:edit] do
collection do
patch 'update_password'
get 'show_payment_history'
end
resource :cart
resources :cart_items , only: [:new, :destroy]
...
...
使用上述路由生成的路径是...
update_password_user PATCH /user/update_password(.:format) users#update_password
show_payment_history_user GET /user/show_payment_history(.:format) users#show_payment_history
user_cart POST /user/cart(.:format) carts#create
new_user_cart GET /user/cart/new(.:format) carts#new
edit_user_cart GET /user/cart/edit(.:format) carts#edit
GET /user/cart(.:format) carts#show
PATCH /user/cart(.:format) carts#update
PUT /user/cart(.:format) carts#update
DELETE /user/cart(.:format) carts#destroy
new_user_cart_item GET /user/cart_items/new(.:format) cart_items#new
user_cart_item DELETE /user/cart_items/:id(.:format) cart_items#destroy
但我仍然无法使其工作,例如,user_cart_path
无法在重定向中工作,不知道...这是什么问题。
对于我建议的解决方案的任何帮助或任何好的方法,我们将不胜感激!
提前致谢
问题与控制器多元化有关name.Once 我将控制器从 cart
重命名为 carts
...一切都已修复。
希望对别人有帮助