Rails 3 表单问题:如何创建“选择”表单与“添加新...”选项?
我想创建一个包含当前现有品牌(例如索尼、松下等)的选择
框。此外,我希望有添加新品牌
选项,这样当用户单击此选项时,会出现一个新的文本字段。
Rails 3 中是否有任何辅助方法可以执行此类操作,或者我需要使用 Javascript 自己实现此操作?
I would like to create a select
box with currently existing brands (e.g. Sony, Panasonic, and so on). In addition, I would like to have Add New Brand
option, such that when user clicks this option, a new text field appears.
Is there any helper methods in Rails 3 that do such thing, or I need to implement this myself using Javascript ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,没有这样的辅助方法。
这是我在 JS 中的做法:
工作示例: http://jsfiddle.net/tCBqA/
To my knowledge there is no such helper method.
Here's how I would do it in JS:
Working example: http://jsfiddle.net/tCBqA/
查看以下视频 @ RailsCasts.com。 Ryan Bates 解释了如何创建嵌套表单,然后使用 jQuery 或 Prototype 动态添加和删除字段。这与您的问题并不完美匹配,但应该能让您朝着正确的方向前进。如果您获得一些运行良好的代码,请考虑将其发布回此问题以供每个人查看。
http://railscasts.com/episodes/196-nested-model-form -part-1
http://railscasts.com/episodes /197-nested-model-form-part-2
Checkout the following videos @ RailsCasts.com. Ryan Bates explains how to create a nested form and then use jQuery or Prototype to add and remove fields dynamically. It isn't a perfect match for you question but should get you headed in the right direction. If you get some code that works well consider posting it back on this question for everyone to see.
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2