# --- app/views/posts/index.html.erb ---
Listing posts
<% form_tag posts_path, :method => 'get', :remote => true do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Name |
Title |
Content |
|
|
|
<%= render @posts %>
<%= link_to 'New Post', new_post_path %>
# --- app/views/posts/_post.html.erb ---
<%= post.name %> |
<%= post.title %> |
<%= post.content %> |
<%= link_to 'Show', post %> |
<%= link_to 'Edit', edit_post_path(post) %> |
<%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %> |
# --- app/views/posts/index.js.erb ---
$(“posts”).update(“<%= escape_javascript(render(@posts)) %>”);
# --- app/models/post.rb ---
class Post < ActiveRecord::Base
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
end
# --- app/controlles/posts_controller.rb ---
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
@posts = Post.search(params[:search])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.js { render :layout => false }
end
#...
end
#...
end