使用 Formtastic 从无线电集中进行选择并可选择创建新记录
过去几天我开始使用 Formtastic,并遇到了第一个令人头疼的问题。
我正在处理的项目每个都有一些关联的帐户。在相关页面上,您需要选择一个收款人帐户。它可以是我们已经知道的帐户(“主要联系人”),也可以是您可以通过填写一些信息来创建的新帐户。
这是我试图复制的设计:
使用 Formtastic 可以非常简单地显示一个表单来输入新帐户的信息。
<%= form.inputs :street, :city, :for => :address %>
创建可用帐户的单选列表也非常简单。
<%= form.input :address, :as => :radio, :collection => @addresses %>
正如上图所示,我困惑的问题是如何同时执行这两项操作 - 选择现有帐户或创建新帐户。
好吧,真正的问题是如何优雅地做到这两点。我想到的任何事情似乎都会向控制器添加逻辑,并让我怀疑是否有一个我没有看到的优雅解决方案(最近深入研究了 RoR,这是一种非常熟悉的感觉)。
I've begun using Formtastic in the last couple days and have come to my first head-scratcher.
I'm working with items that each have a few associated accounts. On the page in question you need to select a payee account. It can either be an account we already know about (the "Main Contact") or a new account that you can create by filling in a little information.
Here's the design I'm trying to replicate:
Using Formtastic it's very simple to display a form to enter a new account's information.
<%= form.inputs :street, :city, :for => :address %>
It's also very simple to create a radio selection list of available accounts.
<%= form.input :address, :as => :radio, :collection => @addresses %>
The problem I'm puzzling over, as the above pic illustrates, is how to do both - select an existing account or create a new one.
Well, the real question is how to do both gracefully. Anything I've thought of seems to add logic to the controller and leaves me suspecting that there's a graceful solution that I'm not seeing (having dived into RoR recently, this is a very familiar feeling).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找不到不会向控制器添加几行的解决方案。尽管“胖模型,瘦控制器”是一个很好的原则,但并不总是可能有“单行”的宁静方法,在顶部有一个 before 过滤器为视图设置变量。
那么我们所说的“优雅程度”是什么? ;) 也许您可以发布您考虑的解决方案,其他成员会对其发表评论或评分?
Can't see a solution that wouldn't add a few lines to a controller. Although "Fat models, thin controllers" is a nice principle, it's not always possible to have "one-liner" restful methods, with a before filter on top setting up variables for views.
So what "level of gracefulness" we are talking about? ;) Maybe you could post a solution you consider and other members would comment on it or rate it?