Rails 页面中的多个视图

发布于 2024-12-17 13:49:08 字数 409 浏览 2 评论 0原文

我有一个非常高水平的问题,我找不到对我来说有意义的答案。我知道这是一个非常广泛的问题,但我只是在寻找一些关于在哪里寻找答案的指示,而不是关于如何构建我的网站的说明。

那么...如果我想使用 Rails 在单个页面中渲染两种不同类型的内容,我将如何去做呢?我该如何格式化 url?假设我创建一个画廊模型和控制器,其中包含有关画廊的信息以及可能的描述,然后我创建一个画廊条目控制器和属于具有图像和图像名称的画廊的模型。如果我想创建一个类似 www.siteURL/galleryName/GalleryEntry 的网址,它既呈现图库信息和描述以及所有关联的图库条目,也呈现画廊条目的更大版本,即在网址中指定我将从哪里开始以及如何构建它?我将如何创建一个具有多个属性的 url 以及如何在控制器/视图中访问它们?

谢谢 - 对于这个含糊的问题感到抱歉

I have a very high level question that I cant find an answer to that makes sense to me. I understand it''s a terribly broad question but I'm only after some pointers in where to look for answers, not instructions on how to build my site.

So... If I want to render two different types of content in a single page using rails, how would I go about doing this? And how would I format the url? Say I create a gallery model and controller which has information about the gallery and perhaps a description, then I create a gallery-entry controller and model that belongs to the gallery which has an image and image name. If I want to create a url something like www.siteURL/galleryName/GalleryEntry that renders both the gallery info and description and all the associated gallery-entries but also a larger version of the gallery-entry that is named in the url where would i start and how would i structure this? How would i go about creating a url that has multiple attributes and how would i access them in the controller/view?

Thanks - and sorry for the vague question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雪若未夕 2024-12-24 13:49:08

有几种方法可以解决这个问题。

您的 URL 看起来像一个“虚荣”URL,除了正常的 RESTful 路由 (gallery/:gallery_id/entries/:entry_id) 之外还存在。不同之处在于您不想显示图库条目。

如果您想专门区分同一资源的不同视图,有多种方法可以完成,我首先考虑的两种方法是添加另一个操作,或添加一个消除歧义的查询参数。在本例中,它是一个混合体,因此我可能会创建一个自定义匹配和控制器方法。

映射可能如下所示:

match ':galleryName/:entryName' => 'gallery#highlight_entry' # Or whatever

操作将是(或多或少):

def highlight_entry
    @gallery = Gallery.find_by_name(...)
    @entries = @gallery.entries
    @highlighted_entry = # pull from @entries, or retrieve
    # Also, filter entries/etc. so the highlighted one doesn't show up
    # etc
end

There's several ways to go about it.

Your URL looks like a "vanity" URL that would exist in addition a normal RESTful route (galleries/:gallery_id/entries/:entry_id). The difference is that you don't want to show just the gallery entry.

If you want to specifically differentiate between different views of the same resource there are a number of ways it could be done, the two I'd consider first are adding another action, or adding a disambiguating query parameter. In this case, it's a hybrid, so I'd probably create a custom match and controller method.

The mapping might look like:

match ':galleryName/:entryName' => 'gallery#highlight_entry' # Or whatever

The action would be (more or less):

def highlight_entry
    @gallery = Gallery.find_by_name(...)
    @entries = @gallery.entries
    @highlighted_entry = # pull from @entries, or retrieve
    # Also, filter entries/etc. so the highlighted one doesn't show up
    # etc
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文