本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7629428
建立时间:2006年5月29日




[Ruby on Rails]Adding a Rich Text Editor to your Rails Application
软件技术

lhwork 发表于 2007/2/7 16:09:39

Adding a rich text editor to your application enables users to markup their input text without having to know a markup syntax such as markdown or html. This page describes how to easily add this functionality to your rails application. There are several free javascript rich text editors that can be incorporated into your application. These work by providing a javascript WYSIWYG interface that allows the user to generate the required html for each element that is enabled in the interface. So for example, if the user selects “XYZ” and presses the bold button, this generates the text <b>XYZ</b> which is saved to the database. Since using these editors allows users to create html that will be input into the database, make sure that you sanitize the html when you are rendering it back from the database. You can use rails’ sanitize method or since you probably want to allow several tags you could use this Ruby sanitize function with the okTags parameter set to whatever set of tags you want to allow. Alternatively check out the whitelist recipe in the rails recipes book. widgEditor widgEditor is a small simple lightweight editor. It provided basic rich functions such as bold, italics, links, lists, images and headings. Its very easy to get setup with your rails application. Download widgEditor Extract the files Copy the widgEditor.js file into your applications public/javascripts folder (you can configure it by changing this file) Copy the widgEditor.css file into your applications public/stylesheets folder. Copy all the images from the widgEditor distribution to your public/images folder. Include the javascript and css files in your layout file. Give any text field you want to be an editor field a class of “widgEditor”. Here is an example of what you might have in your view file: <%= stylesheet_link_tag 'widgEditor' %> <%= javascript_include_tag "widgEditor" %> <%= text_area 'node', 'content', :cols => "50", :rows=>"3", :class=>"widgEditor" %> TinyMCE TinyMCE is a much more full featured rich text exitor. Check out the demo to see all the features that can be enabled for it. It is easy to incorporate TinyMCE into your rails application. Download TinyMCE and place the tiny_mce directory in your applications public/javascripts directory. Include tinymce/tinymce and Call tinyMCE.init with the options that you want enabled. This call controls which of the available TinyMCE elements are enabled. See the TinyMCE documentation or look at the examples in the distribution for a list of all the options. All your text area fields will now be TinyMCE editor instances. Below is an example of what you might have in your layout file. It only enables a small subset of the available TinyMCE widgets. <%= javascript_include_tag 'tiny_mce/tiny_mce' %> <script language="javascript" type="text/javascript"> tinyMCE.init({ mode : "textareas", theme : "advanced", convert_urls : false, plugins : "emotions,preview", theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough, bullist,numlist,link", theme_advanced_buttons2 : "", theme_advanced_buttons3 : "", theme_advanced_toolbar_location : "top", theme_advanced_toolbar_align : "left", extended_valid_elements : "a[name|href|target|title|onclick],img[class|src| border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name], hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]" }); </script> In your view create a text area. <p><label for="blog_content">Content</label><br/> <%= text_area 'blog', 'content', :cols => "50" %></p> TinyMCE includes quite a lot of javascript files. It is a good idea to include these files only on pages that actually use the rich text editor. This will save on bandwidth and make for quicker page loads for the pages that aren’t using the rich text editor. You could do this by having a separate layout for the part of the application that uses the TinyMCE or by adding a condition in your layout file that checks the controller name before deciding to load it. Also worth investigating Another option worth investigating is FCKEditor. I haven’t tried this but it looks a bit more complicated that those discussed above. Update (05/06/06) You can now integrate TinyMCE using the TinyMCE plugin created by Blake Watters. See John Wulff’s tutorial for more details.


阅读全文(3239) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144801251 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号