共享相同控制器的嵌套资源 [英] Nested resources sharing same controller
本文介绍了共享相同控制器的嵌套资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我希望能够管理产品内的卖家佣金或卖家本身,所以我的路线如下所示:
In my application, I want to make it so it's possible to manage sellers commissions within a product or the seller itself, so my routes look like this:
resources :sellers do
resources :commissions
end
resources :products do
resources :commissions
end
我的网址将如下所示:
/products/:product_id/commissions/:id(.:format)
/sellers/:seller_id/commissions(.:format)
但是我如何在我的 CommissionsController 中知道请求是来自产品还是卖家,以便我可以根据 product_id 或 Seller_id 设置我的佣金?
But how can I, inside my CommissionsController know when the request is comming from a Product or a Seller so I can set my commissions based on product_id or seller_id?
多谢指教
推荐答案
您可以只检查是否存在 params[:product_id]
或 params[:seller_id]
:
You can just check for the presence of either params[:product_id]
or params[:seller_id]
:
#app/controllers/commissions_controller.rb
class CommissionsController < ApplicationController
before_action :set_model
def index
@commissions = @model.find @id
end
private
def set_model
case true
when params[:seller_id]
model = "Seller"
when params[:product_id]
model = "Product"
end
@model = model.constantize
@id = model
end
end
这篇关于共享相同控制器的嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文