Rails 中的文本字段大小动态增加
我正在尝试在我的项目中实现类似于 Facebook 的群组功能。我将 Rails 2.0.2 和 Ruby 1.8.7 用于项目特定目的。我使用的是 Ubuntu 10.04 操作系统。
我想为讨论主题和相应的评论实现一个文本字段,其大小随着文本大小的增加而动态增加。
在Rails中,在定义迁移时,有一种方法可以为描述等属性提供一个巨大的文本区域,其代码类似于迁移中的 t.text :description
。
现在,在大多数情况下,讨论主题和相应的评论不是很大,这看起来不太好。
考虑到这一点,是否有任何其他方法可以解决这个问题,其中可以根据运行时输入的文本大小动态控制文本字段。如果是,请指导我如何做同样的事情。
另外,我必须将其命名为什么类型的属性。
对此的任何意见/建议都非常感谢..
谢谢...:)
I am trying to implement a groups feature similar to that of Facebook in my project. I am using Rails 2.0.2 and Ruby 1.8.7 for project specific purposes. I am using Ubuntu 10.04 OS.
I want to implement a text field for the Discussion Topic and the corresponding comments whose size increases dynamically as there is an increase in the size of the text.
In Rails while defining your migrations there is a way of having a huge text area for attributes like Description, the code for it is some thing like this in the migration t.text :description
.
Now this would not look really good in most cases where the discussion topics and the corresponding comments aren't so big.
Considering this, is there any other way around it wherein the the text field can by dynamically controlled based on the size of the text entered during run time. If yes kindly guide me on how can I go about the same.
Also , what type of attribute would I have to name it as for the same.
Any inputs/suggestions on this is highly appreciated..
Thank you...:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,首先我应该指出,迁移文件中的 t.text 不一定是您认为获得大文本区域的原因。 t.text 只是通知 Rails 生成器在给定表中创建一个可以存储大量文本数据的列。
但如果您使用脚手架,生成的视图将使用
f.text_area
在屏幕上渲染相应的字段,这就是它显得很大的原因。现在,如果您想让文本区域保持较小并让它根据内容增长,则需要使用 JavaScript。我最喜欢的 JavaScript 库/framrwork 是 jQuery,如果您熟悉 jQuery,您可以使用 jQuery 插件,例如 jQuery 扩展文本区域 (演示< /a>) 来实现这一目标。然而,Rails 2 默认使用 Prototype/Scriptaculous,如果你想坚持使用它,那么你可以使用类似的 Prototype 插件,例如 这个或者从头开始编写一些东西,例如 这个。
Okay, first I should point out that the
t.text
in the migrations file is not necessarily the reason why in your view you get a large text area.t.text
just informs the rails generator to create a column in the given table that can store large textual data.But if you use scaffolding, the generated view will use
f.text_area
to render the corresponding field on the screen and that's why it appears large.Now if you want to keep the text area small and let it grow according to its content, you need to use JavaScript. And my favorite JavaScript library/framrwork is jQuery and if you are familiar with jQuery you can the use a jQuery plugin like the jQuery expanding text area (demo) to achieve that. However Rails 2 uses Prototype/Scriptaculous by default and if you want to stick with that, then you can use a similar Prototype plugin such as this one or write something from scratch like this.