一页模型,不同的布局/视图。我如何利用性传播感染?

发布于 2024-10-07 03:09:34 字数 884 浏览 0 评论 0原文

我正在构建一个应用程序来学习 Rails。这是一个简单的页面管理器,使用 Awesome_nested_set 创建嵌套页面。我想使用 STI,但不确定如何实现。我看到的大多数 STI 示例都涉及主“宠物”模型以及“狗”和“猫”的子模型;我很难将其变成现实世界的例子。这是我使用页面的方式。

3 种不同的页面“类型”:博客、照片、手机照片。真正描述这一点的唯一方法是向您展示我当前在 Coldfusion 中构建的网站博客照片手机照片

所有这些都使用相同的表和几乎相同的属性。照片部分使用照片属性,并有一些细微的变化,例如上传、调整大小等——但除此之外,不同“类型”之间的一切都是相同的。页面类型之间的主要区别在于页面的布局方式以及访问方式。例如:

在索引上:
/photos - 布局缩略图,显示所有带有“照片”类型的页面
/blog - 显示所有带有“博客”类型的页面的布局

在节目中:
/photos/1 - 显示带有上一张/下一张照片的大照片
/blog/2 - 显示带有上一个/下一个条目的博客条目

我需要为每种类型使用单独的控制器吗?如果我不需要对页面的创建/更新方式进行任何更改,我是否需要单独的模型?我是否只创建指向呈现我需要的布局的操作的路由?我认为我看到了“性传播感染”这个词,并且让它变得比本来应该的更加困难。我只是很谨慎,因为我想学习“rails way”。我还需要性传播感染吗?

任何帮助将不胜感激,我只需要克服这个驼峰,有人帮助使它“点击”! :)

I have an app I'm building to learn rails. It's a simple page manager that uses awesome_nested_set to create nested pages. I would like to use STI but am unsure on how to make that work. Most of the examples I see of STI involve a main "Pets" model and sub models of "Dog" and "Cat"; I'm having a hard time making that into a real world example. Here's how I am using the pages.

3 different page "types": Blog, Photo, Mobile Photos. Only way to really describe this is to show you my current site built in Coldfusion Blog, Photo, Mobile Photos.

All use the same table and pretty much the same attributes. The photo sections use a photo attribute and have some minor changes such as uploading, resizing, etc -- but other than that everything is the same between the different "types". The main difference between the page types is how the page will be laid out and how they are accessed. For example:

On the index:
/photos - layout thumbnails to show all pages w/ a type of "Photo"
/blog - layout to show all pages w/ a type of "Blog"

On the show:
/photos/1 - show large photo w/ prev/next photo
/blog/2 - show blog entry w/ prev/next entry

Do I need a separate controller for each type? Do I need separate models if I don't need any changes in the way pages are created/updated? Do I just create routes that point to an action which renders the layout I need? I think I'm seeing the term "STI" and making it more difficult than it has to be. I'm just being cautious because I want to learn the "rails way". Do I even need STI?

Any help would be greatly appreciated, I just need to get over this hump, someone help make it "click"! :)

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

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

发布评论

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

评论(1

若相惜即相离 2024-10-14 03:09:34

我在这里写了一篇博客文章将指导您逐步完成性传播感染。

就 Photo 类而言,您必须上传图像,您可以执行此操作。

class Photo < Page

attr_accessor :photo_file_name, :photo_file_size, :photo_file_type

  has_attached_file :photo,
                  :url => "#{your_upload_url}",
                  :path => "#{your_upload_path}"
end

如果您使用此代码,您的页面模型中必须有一列名为 photo 的列,该列将存储上传文件的 file_name 。

希望这对您有帮助。如果您需要更多建议,我就在这里。 :D

更新

再次阅读问题后,我意识到您还需要为所有子 STI 课程提供单独的页面。因此,您将需要不同的控制器来处理该问题。

Here I have written a blog post which will guide you step by step in accomplishing STI.

As far as the Photo class goes where you have to upload images you can do this

class Photo < Page

attr_accessor :photo_file_name, :photo_file_size, :photo_file_type

  has_attached_file :photo,
                  :url => "#{your_upload_url}",
                  :path => "#{your_upload_path}"
end

If you use this code you must have a column with name photo in you Page model that will store the file_name of the uploaded file.

Hope this helped you. I am here if you need more suggestions on this. :D

UPDATE

After reading the question once again I realised that you also need a separate page for all the child STI classes. Hence you will be needing different controllers to handle that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文