项目MVC结构的问题

发布于 2024-09-15 04:49:13 字数 681 浏览 5 评论 0原文

在我的项目中,我有大约 20 个 html 表单,其中有许多不同的 。这个 在每种形式中都是唯一的,并且它们之间不会重复。对于每个表单都有一个脚本,它从表单获取数据,生成特定文件,并将其推送到浏览器。就是这样。没有数据库、管理、登录名/密码和其他常见的网络应用程序内容。

例如,在 php 中,项目结构可以是这样的:

forms/

------->form1/

--------------->index.html

--- ------------>脚本/

----------------------->index.php

------ ->form2/

--------------->index.html

-------------->script/

------ ----------------->index.php

等。它非常清晰,并且可以生成漂亮的 url,例如:

www.website.com/forms/form1

,但在 Ruby-on-Rails 中存在 MVC 模式。我不知道如何组织这样的项目的结构。怎样做才对呢?毕竟我不应该制作 20 个不同的控制器,对吗?

in my project i have about 20 html forms with many different <input>'s. This <input>'s are unique in every form and they don't repeated between them. For each form there is a script which get data from form, generate specific file, and push it to the browser. and that's it. no databases, admin, logins/passwords and other usual web-app stuff.

so for example in php, project structure can be something like this:

forms/

------->form1/

--------------->index.html

--------------->script/

----------------------->index.php

------->form2/

--------------->index.html

--------------->script/

----------------------->index.php

and so on. It's quite clear and it makes pretty urls like:

www.website.com/forms/form1

but in Ruby-on-Rails there is a MVC pattern. And i have no idea how to organize structure with project like that. How to make it right? I should not to make 20 different controllers after all, right?

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

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

发布评论

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

评论(2

默嘫て 2024-09-22 04:49:13

是的,您确实制作了 20 个控制器。与 PHP 结构中有 20 个 script/index.php 文件的方式大致相同。 Rails 结构看起来像这样。

app/
    controllers/
        form1s_controller
        form2s_controller
        .....
        formns_controller
    view/
        form1/
            new.html.erb
        form2/
            new.html.erb
        ......
        formn/
            new.html.erb
        layouts/
            application.html.erb

其中控制器为您想要执行的每个操作(例如新建、编辑等)提供了一个方法

。layouts/application.html.erb 文件将包含所有页面中一致的所有标记。

最后,不要试图与结构作斗争。由于约定,它可以启用最少的代码,如果您尝试创建自己的结构,您将创建一个痛苦的世界。

Yes you do make 20 controllers. In much the same way as you have 20 script/index.php files in your PHP structure. The rails structure would look something like this.

app/
    controllers/
        form1s_controller
        form2s_controller
        .....
        formns_controller
    view/
        form1/
            new.html.erb
        form2/
            new.html.erb
        ......
        formn/
            new.html.erb
        layouts/
            application.html.erb

Where the controllers have a method for each action that you want to perform e.g. new, edit etc

The layouts/application.html.erb file will contain all of the markup that is consistent across all of your pages.

Finally, don't try and fight the structure. It is there to enable minimal code because of convention, if you try and create your own structures you will create a world of pain.

注定孤独终老 2024-09-22 04:49:13

由于表单没有数据库后端,我将创建一个 FormsController,其中包含 20 个方法(form1、form2、form3,...)。

然后,您的网址看起来几乎相同 \forms\form1\forms\form2 ...

即使您使用 20 个控制器,Ruby 中也有很多方法可以删除重复的代码。因此,即使在普通的 MVC 中,我也有过包含 140 个模型的应用程序,并且同样有许多控制器,其中大多数控制器只有一行,并且视图完全是通用的。

仅仅因为某个解决方案在 PHP 中看起来很简单,并不意味着不存在更好的解决方案。比如MVC。它可能看起来会导致更多的代码、更多的文件、更多的工作,但结构的存在是有充分理由的,它干净地分离了关注点,并且每个文件本身变得更加清晰和更容易理解。

Since there is no database backend for the forms, i would create one FormsController, with 20 methods (form1, form2, form3, ...).

Your urls would then look almost the same \forms\form1, \forms\form2 ...

Even if you would use 20 controllers, there are many ways in ruby to remove duplicate code. So even in a normal MVC, i have had applications with 140 models, and likewise as many controllers, most of these controllers had only one line and the views were completely generic.

Just because a certain solution seemed simple in PHP, it doesn't mean better solutions don't exist. Like for instance MVC. It might seem to induce more code, more files, more work, but the structure is there for good reason, it seperates the concerns cleanly, and each file in itself becomes more clear and easier to understand.

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