简体   繁体   English

Rails页面中的多个视图

[英]multiple views in a rails page

I have a very high level question that I cant find an answer to that makes sense to me. 我有一个非常高层次的问题,我找不到适合我的答案。 I understand it''sa 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? 所以...如果我想使用Rails在一个页面中呈现两种不同类型的内容,我该怎么做呢? 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? 如果我想创建一个类似www.siteURL/galleryName/GalleryEntry的URL,它既可以显示图库信息和描述以及所有相关的图库条目,又可以显示在该URL中命名的更大版本的图库条目。开始,我将如何构造它? 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 谢谢-并对模糊的问题表示抱歉

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 ). 您的URL看起来像是一个“虚荣” URL,除了正常的RESTful路由( 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. 在这种情况下,它是混合的,所以我可能会创建一个自定义的match和controller方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM