[英]Map one controller action on another action rails
I have a controller named carts_controller
and in my routes I am using restful routes ie, resources :carts
. 我有一个名为
carts_controller
的控制器,在我的路由中,我使用的是carts_controller
路由,即resources :carts
。
I know resources create default actions like create, index etc., but if I don't want to user create and create a method add_to_cart
and in routes I have defined its route as 我知道资源会创建默认操作,例如创建,索引等,但是如果我不想用户创建和创建方法
add_to_cart
并且在路由中我将其路由定义为
post '/add_cart/:product_id/' => 'carts#add_to_cart', as: 'add_to_cart'
Does this route considered RESTFUL? 这条路线是否被认为是RESTFUL?
I don't want to user all the default RESTFUL
routes created by resources. 我不想使用资源创建的所有默认
RESTFUL
路由。 I want some custom actions in place of these. 我想要一些自定义动作来代替这些动作。 My code is working but I am confused as my concepts are not clear.
我的代码正在运行,但是由于我的概念不清楚,我感到困惑。 Another thing is if I know that I need
product_id
in my routes, should I make them nested inside products resources or it will work if I define custom ad I defined above? 另一件事是,如果我知道自己的路线中需要
product_id
,是否应该将它们嵌套在产品资源中,或者如果我定义了上面定义的自定义广告,它是否可以工作?
Any help would be appreciated! 任何帮助,将不胜感激!
I think your current approach is fine. 我认为您目前的做法很好。 Not all controller actions will fit nicely into the standard CREATE/UPDATE/DESTROY actions.
并非所有的控制器动作都能很好地适合标准的CREATE / UPDATE / DESTROY动作。 It's also pretty obvious what
add_to_cart
does. add_to_cart
的作用也很明显。
As an alternative you could consider doing this in the update
action of the cart controller. 或者,您可以考虑在购物车控制器的
update
操作中执行此操作。 If a cart
has many products
you could consider using nested params: 如果
cart
有很多products
您可以考虑使用嵌套参数:
params: {
cart: {
products_attributes: [{
"0" => { ...product_attributes_here.. }
},
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.