使用嵌套资源干燥 Rails 视图

发布于 2024-08-26 07:36:37 字数 812 浏览 13 评论 0原文

如果您有一个非嵌套和嵌套的模型(例如产品),您的问题解决方案是什么:

“产品”可以属于“事件”,并且产品也可以是独立的。

这意味着我可以有这样的路线:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2024-09-02 07:36:37

我建议您使用自己的视图创建单独的管理控制器来管理您想要的所有内容。而你的客户的逻辑留在产品控制器中。

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.

预谋 2024-09-02 07:36:37

对于这个问题我没有好的、干净的解决方案。通常,如果视图差异不大,我会使用单个视图并添加一些代码,例如 @product.event.nil?。您始终可以添加一些变量或帮助器来使该方法更短,例如 has_event? - 那么您的视图会看起来更干净。并在如下代码中使用它:

<% if has_event? %>
  some html
<% end %>

或者对于单行:

<%= link_to 'Something special', foo_path if has_event? %>

另一方面,您可以为两个视图创建一些相同的部分,将它们放在某个文件夹中,例如 /shared/products/... 并从您的视图中渲染它们,如下所示:

<%= render :partial => '/shared/products/info' %>

等等。

但如果它们没有太大差异,我真的会使用 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 example has_event? - then your view will look cleaner. And use it in code like this:

<% if has_event? %>
  some html
<% end %>

or for single line:

<%= link_to 'Something special', foo_path if has_event? %>

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:

<%= render :partial => '/shared/products/info' %>

and so on.

But if they don't differ too much, I really would use if version.

北风几吹夏 2024-09-02 07:36:37

视图将由 ProductsController 处理。您可以根据资源的嵌套来更改控制器中的逻辑。

# app/controller/products_controller.rb
# ...some code...
def index
    @event = Event.find_by_id(params[:event_id]) if params[:event_id]
    @products = @event ? @event.products : Product.all
end

该视图将由通常的产品视图处理

# app/views/products/index.html.haml
- unless @products.blank?
    - @products.each do |product|
        %p= product.some_attribute

The views will be handled by the ProductsController. You can alter the logic in your controller depending on the nesting of the resource.

# app/controller/products_controller.rb
# ...some code...
def index
    @event = Event.find_by_id(params[:event_id]) if params[:event_id]
    @products = @event ? @event.products : Product.all
end

The view will be handled by the usual product view

# app/views/products/index.html.haml
- unless @products.blank?
    - @products.each do |product|
        %p= product.some_attribute
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文