rails之FCKEditor插件(转载)

环境:ubuntu linux 7.0.4  +  ruby 1.8.5 + Rails 1.2.3 + FCKeditor 0.4.1

直接从它的subversion库里取得该Rails插件。先进入到你的项目根目录,再执行如下命令

ruby script/plugin install svn://rubyforge.org/var/svn/fckeditorp/trunk/fckeditor


其他说明:
(1)你的linux必须先安装了subversion。(ubuntu里用新立得搜subversion即得)
(2)把命令中的install 改为 destory,可以删除安装。
(3)我取到的是2007年4月份最后更新的, v 0.4.1版
(4)FCKeditor安装在项目根目录下的vendor/plugins/fckeditor 里,
(5)vendor/plugins/fckeditor里的README很值得一读,我碰到Ajax问题,查了所以网上的中文资料都没有提到,在这个README却有。


用如下语句在页面里含入它的javascript库

<%= javascript_include_tag :fckeditor %>  


<%= javascript_include_tag "fckeditor/fckeditor" %>



在需要富文本的Form表单用如下语句生成一个富文件编辑框:

<%= fckeditor_textarea("topic""content", :toolbarSet => 'Simple', :width => '100%', :height => '200px') %>


说明:topic对应模型对象,content对应它的字段。也就是要求当前页要有@topic这个实例变量。


如果是用了ajax,则需要在form_remote_tag加上一个before项

  <%= form_remote_tag(:update => update,
                      :before 
=> fckeditor_before_js('topic', 'content'),
                      :url 
=> {:controller => 'topics', :action => 'create', :template => 'show'} )%>


并且富文件编辑框要加一个ajax=true的选项:

<%= fckeditor_textarea(:topic, :content, :ajax => true:toolbarKit => 'Simple', :width => '100%', :height => '600px') %>



在使上传图片的功能时碰到了错误。弹出出alert对话框,显示:Error on file upload.Error number: 403

在日志里显示如下,表面上看好象是路由配置的问题

ActionController::RoutingError (no route found to match "/fckblank.html" with {:method=>:get}):
/var/lib/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1292:in `recognize_path'
/var/lib/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1282:in `recognize'
/var/lib/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:40:in `dispatch'
/var/lib/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:113:in `handle_dispatch'
/var/lib/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'


最后在http://blog.caronsoftware.com/articles/2006/12/03/fckeditor-0-4-0-released#comment-1745找到了答案,这是一个BUG

解决方法 :

修改:vendor/plugins/fckeditor/app/controller/fckeditor_controller.rb
将原来的

unless "#{file.class}" == "Tempfile"


改为

unless "#{file.class}" == "Tempfile" || "#{file.class}" == "StringIO"

Rails分页plugin之will_paginate(转载)

搜索结果的分页显示是一个常用功能,实现方式有很多中,rails也内建了对分页的支持,但是rails2.0将去掉分页组件,将分页功能交给plugin去实现,这里通过一个简单的例子展示如何使用最流行的rails分页plugin -- will_paginate.

1.创建rails工程及数据库

CODE:

rails demo
mysql>create database demo_development;
项目名为demo,开发数据库为demo_development
自行修改config/database.yml配置数据库密码,如果root密码为空,则不需要修改

2.生成脚手架和数据
创建一个product的脚手架代码

CODE:

ruby script\generate scaffold_resource product name:string, description:string,created_at:datetime运行db/migrate创建数据表

CODE:

rake db:migrate构造测试数据

CODE:

ruby script\console
>>0.upto(30) do |i|
>>  Product.create(:name => "product_#{i}", :description => "product number is #{i}")
>>end
启动服务

CODE:

ruby script\server访问http://127.0.0.1:3000/products 就可以看到所有的产品了,模式是全部显示,没有分页.

3.安装will_paginate,编写分页代码
安装:

CODE:

ruby script\plugin install svn://errtheblog.com/svn/plugins/will_paginate安装成功后,开始编写分页代码
修改products_controller.rb,找到index方法

CODE:

    #@products = Product.find(:all)  #把这行注释掉
    @products = Product.paginate  :page => params[:page],
                                  :per_page => 5
保存后刷新http://127.0.0.1:3000/products 可以看到现在只显示前5个产品了
加入翻页,修改app/view/products/index.rhtml,在最后加入一行

CODE:

<%= will_paginate @products %>再次刷新页面,可以看到翻页了,试着点击next,previous进行翻页,可以看到url类似http://127.0.0.1:3000/products?page=4

4.加入搜索
在index.rhtml适当位置加入:

CODE:

<% form_tag products_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>
这里注意,:method => 'get'必须指定, submit_tag的:name属性设为nil,避免在url中出现不必要的内容

修改products_controller.rb的index方法

CODE:

    #@products = Product.find(:all)
    @products = Product.paginate  :page => params[:page],
                                  :per_page => 5,
                                  :conditions => ["name like ?", "%#{params[:search]}%"]
注意在加入搜索条件时的写法,这样可以避免sql注入,提高安全性

试着进行搜索,可以看到搜索结果很好的进行了分页.