简体   繁体   English

将一个控制器动作映射到另一个动作导轨上

[英]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.

相关问题 如何显示一个控制器在导轨中的另一个控制器动作中显示动作 - how to display one controller show action in another controller action in rails 从一个控制器调用Rails 5动作 - Calling a Rails 5 action from one controller to another Rails 3-与Kaminari分页-显示一个控制器的动作,浏览另一个控制器的记录 - Rails 3 - Pagination with Kaminari - Show action of one controller, to browse records of another Rails:从一个控制器动作重定向到另一个(非GET) - Rails: Redirecting from one controller action to another (non-GET) 在Rails中从一个应用程序到另一个应用程序调用控制器操作 - Call controller action from one application to another in Rails 在RAILS中将模型从一个控制器动作传递到另一个控制器动作的替代方法 - Alternative ways to pass model from one controller action to another in RAILS Rails 3.2 将参数从一个控制器动作传递到另一个控制器动作 - Rails 3.2 passing parameters from one controller action to another Rails:如何在同一个控制器中将值从一个动作传递到另一个动作 - Rails: How to pass value from one action to another in the same controller Rails - 如何避免必须从另一个控制器中的create action触发一个控制器中的create操作 - Rails - how do I avoid having to trigger the create action in one controller from the create action in another controller 一个控制器在另一个控制器上的CRUD动作 - CRUD action of one controller in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM