ruby-on-rails - rails 3 : How to render an action from another controller's view

标签 ruby-on-rails controller action render

我正在尝试从不同 Controller 的 View 中调用一个操作:

Controller Countries 有一个名为 selectbox 的操作,它从所有国家/地区生成一个 html 选择框作为部分。
Controller Customers 有一个名为 new 的操作,它为所有客户属性生成一个 html 表单。我想在此表单中插入来自 Countries#selectbox 的选择框。

实现这一目标的正确方法是什么?

最佳答案

你做错了。如果有一段代码要重用(例如生成 html 选择框),则应将其放在帮助程序中和/或使用部分代码来呈现 html 选择框部分。

请记住,只有在代码有些复杂(例如,超过一两行)时,这才是好的建议。这是我在谷歌搜索时发现的一篇可能对您有帮助的帖子:helper or partial

关于ruby-on-rails - rails 3 : How to render an action from another controller's view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5831756/

相关文章:

ruby-on-rails - 如何在 Rails 2.3.5 中安装/使用 Devise?

ruby-on-rails - 带有 If 条件的 jbuilder

ruby-on-rails - 非常长的 html 属性的 Haml 多行

ruby-on-rails - 如何使用 rspec 在 Controller 中测试自定义路由

JavaFX - 访问动态创建的选项卡 Controller

ios - 我们可以快速为 2 个 Action 制作 1 个 UIButton 吗?

android - 为什么我的可搜索 Activity 的 Intent.getAction() 为空?

ruby-on-rails - Rabl json 集合动态键

asp.net - 如何在 Asp.net core 中将列表从 View 发布到映射到 viewModel 的 Controller ?

swift - 处理两种文档类型