使用嵌套资源干燥 Rails 视图
如果您有一个非嵌套和嵌套的模型(例如产品),您的问题解决方案是什么:
“产品”可以属于“事件”,并且产品也可以是独立的。
这意味着我可以有这样的路线:
map.resources :products # /products
map.resources :events do |event|
event.resources :products # /events/1/products
end
你如何在你的视图中正确处理它?
注意:这是针对管理面板的。我希望能够有一个“创建活动”页面,其中包含一个用于创建票证(产品)、表格以及检查谁已回复的侧面板。因此,您可以点击“活动门票”侧面板按钮,然后您将进入 /events/my-new-event/tickets
。但管理面板还有一个根“产品”选项卡,可以列出门票和其他随机产品。 “门票”和“产品”视图看起来 90% 相同,但门票将包含有关其所属活动的一些信息。
看来我必须具有这样的视图:
- products/index.html。 haml
- products/show.haml
- events/products/index.haml
- events/products/show.haml
但这似乎并不干燥。或者我可以使用条件检查来查看产品是否有事件 (@product.event.nil?
),但视图将很难理解。
你如何处理这些情况?
非常感谢。
What is your solution to the problem if you have a model that is both not-nested and nested, such as products:
a "Product" can belong_to say an "Event", and a Product can also just be independent.
This means I can have routes like this:
map.resources :products # /products
map.resources :events do |event|
event.resources :products # /events/1/products
end
How do you handle that in your views properly?
Note: this is for an admin panel. I want to be able to have a "Create Event" page, with a side panel for creating tickets (Product), forms, and checking who's rsvp'd. So you'd click on the "Event Tickets" side panel button, and it'd take you to /events/my-new-event/tickets
. But there's also a root "Products" tab for the admin panel, which could list tickets and other random products. The 'tickets' and 'products' views look 90% the same, but the tickets will have some info about the event it belongs to.
It seems like I'd have to have views like this:
- products/index.haml
- products/show.haml
- events/products/index.haml
- events/products/show.haml
But that doesn't seem DRY. Or I could have conditionals checking to see if the product had an Event (@product.event.nil?
), but then the views would be hard to understand.
How do you deal with these situations?
Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您使用自己的视图创建单独的管理控制器来管理您想要的所有内容。而你的客户的逻辑留在产品控制器中。
I recommend you to make separate admin controller with it's own views to administrate everything you want. And your customer's logic stayed in products contoller.
对于这个问题我没有好的、干净的解决方案。通常,如果视图差异不大,我会使用单个视图并添加一些代码,例如
@product.event.nil?
。您始终可以添加一些变量或帮助器来使该方法更短,例如has_event?
- 那么您的视图会看起来更干净。并在如下代码中使用它:或者对于单行:
另一方面,您可以为两个视图创建一些相同的部分,将它们放在某个文件夹中,例如
/shared/products/...
并从您的视图中渲染它们,如下所示:等等。
但如果它们没有太大差异,我真的会使用
if
版本。I don't have good and clean solution for this problem. Usualy if views doesn't differ to much, I use single view and add some code like
@product.event.nil?
. You can always add some variable, or helper that will make this method shorter, on examplehas_event?
- then your view will look cleaner. And use it in code like this:or for single line:
On the other side, you can create few partials that are the same for both views, put them in some folder, on example
/shared/products/...
and render them from your views like this:and so on.
But if they don't differ too much, I really would use
if
version.视图将由
ProductsController
处理。您可以根据资源的嵌套来更改控制器中的逻辑。该视图将由通常的产品视图处理
The views will be handled by the
ProductsController
. You can alter the logic in your controller depending on the nesting of the resource.The view will be handled by the usual product view