http://rorwiki.hellopen.net/index.php?title=%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8ajax%E5%81%9A%E5%AE%9E%E6%97%B6%E6%9F%A5%E8%AF%A2
1.引入javascript类库。
使用<%=define_javascript_functions%>或者<%= javascript_include_tag :defaults %>
2.往controller新增一个方法list
def list
if @params['search']
@items_pages, @items = paginate :items,
:order_by => 'description',
:conditions => [ 'LOWER(description) LIKE ?',
'%' + @params['search'].downcase + '%' ],
:per_page => 20
@mark_term = @params['search']
render_without_layout
else
@items_pages, @items = paginate :items,
:order_by => 'description', :per_page => 20
end
end
3.新建一个list.rhtml
<div id="target_id">
</div>
<input id="search" name="search" type="text" value="">
<%= observe_field 'search', :frequency => 0.5,
:update => 'target_id', :url =>
{ :controller => '<controller>', :action=> 'list' },
:with => "'search=' + escape(value)" %>
4.使用ajax来分页,在list.rhtml页面增加
<%= link_to_remote(h('< Previous'),
:update => 'target_id',
:url => { :page => paginator.current.previous }) %>
-
<%= link_to_remote(h('Next >'),
:update => 'target_id',
:url => { :page => paginator.current.next }) %>