gpt4 book ai didi

ruby-on-rails - 如何在 Rails 中将两个 Controller 与一个模型一起使用

转载 作者:行者123 更新时间:2023-12-03 16:03:50 27 4
gpt4 key购买 nike

我有一个订单模型。客户通过 Orders Controller 与 Order 模型交互。管理员通过 Purchases Controller 与 Order 模型交互。

大多数情况下它都在工作,除非发生这种情况:

  • 管理员用户转到 new_purchase_path
  • 该应用程序使用购买 Controller 中的“创建”操作,如
    预期
  • 然后应用程序使用订单 Controller 中的"new"操作(而不是
    购买 Controller )
  • 应用程序然后呈现“应用程序/购买/新” View (不是
    “app/orders/new” View ),尽管它已经切换到使用订单 Controller
  • 管理员创建订单后,应用程序将呈现
    使用订单 Controller 的“app/orders/show” View

  • 我真正需要发生的是:
  • 管理员用户转到 new_purchase_path
  • 然后应用程序使用购买 Controller 中的“创建”操作
  • 然后应用程序使用购买 Controller 中的"new"操作
  • 然后应用程序呈现“应用程序/购买/新” View
  • 管理员创建订单后,应用程序将呈现
    使用购买 Controller 的“app/purchases/show” View

  • 在 app/controllers/purchases_controller.rb 我有这个:
      def new
    @purchase = Order.new
    respond_with @purchase
    end

    如果尝试过类似的变化......
      def new
    @purchase = Order.new
    respond_with @purchase, :controller => :purchases
    end

    ...但没有任何类似的记录为 response_with 记录,自然它不起作用。我能做什么?

    最佳答案

    一些观察:

  • 您不应该在新操作中使用 *respond_with*,因为它对 HTML 以外的任何东西都没有意义。
  • 如果您只想为管理员制作一些 Controller ,您应该使用命名空间。见 http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing .这样您也不需要更改名称。
  • 关于ruby-on-rails - 如何在 Rails 中将两个 Controller 与一个模型一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13682312/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com