如何使用单个表继承的模型生成表单?

发布于 2025-02-03 07:16:37 字数 1181 浏览 4 评论 0原文

我有一个具有类型字段的模型公司。有两个子类舰队和经销商。

我知道继承正在起作用,所以我现在也努力努力努力。但是,当我在网页上单击特定的公司对象上的“ firess_erb:fivief/companies/_form.html.erb”在网页上的编辑时,正在产生

undefined method `dealer_path' for #<ActionView::Base:0x000000000418c0>

错误

<%= form_with(model: company, class: "contents") do |form| %>
    *CONTENT*
<%end%>

此 对于所有填充其相应数据的字段。

我在ROR站点上发现了此警告( https://guides.rubyonrails.orgy.org.orgs.orforms.orform_helpers.helpers.helpers.html ):

与模型一起使用STI(单桌继承)时,如果仅声明其父类资源,则不能依靠子类上的记录标识。您将必须指定:url和:范围(模型名称)明确。

但是我不明白这意味着什么,我相信这是我解决方案的关键。我希望在该指南中有一个例子。

欢迎和赞赏任何帮助或建议!

更新

我更改了form_with标签为:

<%= form_with(model: [:company @company], class: "contents") do |form| %>

从我的理解中,现在生成URL Companies/:ID/EDIT

更接近,现在将我带到公司编辑页面,但我无法更新现在,因为变化不持续。

我现在的新问题是,我应该设置哪些路线? 如果我在Localhost上:3000/Companies和我单击“显示公司”,这是否应该将我带到/companies/id:或带我去/dealer/id://fleet/id:

I have a model Company that has a type field. There are two subclasses Fleet and Dealer.

I know the inheritance is working so I'm now working towards the views working too. However the views/companies/_form.html.erb is producing this error when I click edit on a specific company object on my webpage:

undefined method `dealer_path' for #<ActionView::Base:0x000000000418c0>

Here is the form_with tag:

<%= form_with(model: company, class: "contents") do |form| %>
    *CONTENT*
<%end%>

I want to generate the form based on what company type I am looking at and for all the fields to be populated with their corresponding data.

I found this warning on the ROR site (https://guides.rubyonrails.org/form_helpers.html):

When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify :url, and :scope (the model name) explicitly.

But I do not understand exactly what this means and I believe it is the key to my solution. I wish there was an example of this situation on that guide.

Any help or suggestions are welcome and appreciated!

UPDATE

I changed the form_with tag to be:

<%= form_with(model: [:company @company], class: "contents") do |form| %>

and from my understanding this now generates the URL companies/:id/edit

This is closer, and now brings me to the companies edit page but I can't update now because the changes don't persist.

My NEW question now is, what routes should I have set up?
If I'm on localhost:3000/companies and I click 'show company', should this take me to /companies/id: or take me to /dealer/id: / /fleet/id: ?

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

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

发布评论

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

评论(1

勿忘初心 2025-02-10 07:16:37

我已经设法将应用程序路由路由,因此无论对象的类型值如何,我都会将我转到 /公司 /ID。这效果很好,现在所有的Crud也工作得很好。

我这次没有登录我这次发生了什么变化,因为我只是恢复了以前的提交并从那里重建。

I have managed to route the app so it will take me to /companies/id regardless of the type value of the object. This works well and all CRUD is working well now too.

I didn't log exactly what it was I changed this time as I just reverted to a previous commit and rebuilt from there.

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