1.下载
fckeditor_plugin-0.3.2.zip,目前版本为0.3.2
2.解压到vendor\plugins目录下,并且重新命名为fckeditor
3.到该应用程序根目录下,然后运行rake fckeditor:install,则执行自动安装
4.在自己的view层中添加
<%= javascript_include_tag :fckeditor %>
以及在你需要编辑的字段textarea替换为
<%= fckeditor_textarea("book", "desc", { :toolbarKit => 'Simple', :width => '100%', :height => '200px' }) %>
第一个参数为你的domain对象,desc为你的编辑字段值,其他顾名思义
然后运行你的页面程序,发现simple upload有点bug,上传后javascript报错
5.追踪代码发现
vendor\plugins\fckeditor\app\controllers\fckeditor_controller.rb下的
def upload
self.upload_file
end 调用了upload_file方法
def upload_file
@new_file = params[:NewFile]
@url = upload_directory_path
begin
ftype = @new_file.content_type.strip
if ! MIME_TYPES.include?(ftype)
@errorNumber = 202
puts "#{ftype} is invalid MIME type"
raise "#{ftype} is invalid MIME type"
else
path = current_directory_path + "/" + @new_file.original_filename
File.open(path,"wb",0664) do |fp|
FileUtils.copy_stream(@new_file, fp)
end
@errorNumber = 0
end
rescue => e
@errorNumber = 110 if @errorNumber.nil?
end
# Fix provided by Nicola Piccinini -- http://superfluo.org
render :text => %Q'<script>window.parent.frames[\'frmUpload\'].OnUploadCompleted(#{@errorNumber});</script>'
#render :inline => 'page << "window.parent.frames[\'frmUpload\'].OnUploadCompleted(#{@errorNumber}, \'#{@new_file}\');"', :type => :rjs
end 中的
render :text => %Q'<script>window.parent.frames[\'frmUpload\'].OnUploadCompleted(#{@errorNumber});</script>' 在浏览服务器时是正常的,但是在快速上传中不应该返回这个script语句
则修改upload方法
def upload
@new_file = params[:NewFile]
@url = upload_directory_path
begin
ftype = @new_file.content_type.strip
if ! MIME_TYPES.include?(ftype)
@errorNumber = 202
puts "#{ftype} is invalid MIME type"
raise "#{ftype} is invalid MIME type"
else
path = current_directory_path + "/" + @new_file.original_filename
File.open(path,"wb",0664) do |fp|
FileUtils.copy_stream(@new_file, fp)
end
@errorNumber = 0
end
rescue => e
@errorNumber = 110 if @errorNumber.nil?
end
# Fix provided by Nicola Piccinini -- http://superfluo.org
render :text => %Q'<script>window.parent.OnUploadCompleted(#{@errorNumber},\"#{UPLOADED}/#{params[:Type]}\",\"#{@new_file.original_filename}\",\"\");</script>'
end 快速上传问题修复
6.发现文件夹里边如果上传图片过多不好备份,故采用/年/月方式保存
修改代码如下:
def upload
@new_file = params[:NewFile]
@url = upload_directory_path
begin
ftype = @new_file.content_type.strip
if ! MIME_TYPES.include?(ftype)
@errorNumber = 202
puts "#{ftype} is invalid MIME type"
raise "#{ftype} is invalid MIME type"
else
path = date_directory_path + "/" + @new_file.original_filename
File.open(path,"wb",0664) do |fp|
FileUtils.copy_stream(@new_file, fp)
end
@errorNumber = 0
end
rescue => e
@errorNumber = 110 if @errorNumber.nil?
end
# Fix provided by Nicola Piccinini -- http://superfluo.org
render :text => %Q'<script>window.parent.OnUploadCompleted(#{@errorNumber},\"#{UPLOADED}/#{params[:Type]}/#{Time.now.year}/#{Time.now.month}/#{@new_file.original_filename}\",\"#{@new_file.original_filename}\",\"\");</script>'
end
private
def date_directory_path
base_dir = "#{UPLOADED_ROOT}/#{params[:Type]}/#{Time.now.year}/#{Time.now.month}"
#Dir.mkdir(base_dir,0775) unless File.exists?(base_dir)
FileUtils.mkdir_p base_dir
"#{base_dir}"
end
7.同理可以对其上传文件名称进行随机处理以防重名,就不多说了。
做此笔记,抛砖引玉。
发现rails的plugin机制挺不错的,比较灵活,不过网上介绍plugin的文章真的不是很多
posted on 2006-11-04 01:29
老妖 阅读(4366)
评论(11) 编辑 收藏 所属分类:
rails