Ok! Now lets add another model to our application to track user status. Type this command from your application root
% merb-gen model statusThis will create model named status.rb in app/models directory as well as migration file named 002_add_model_statuses.rb, Go on and edit this file to create the statuses table.
class AddModelStatuses < Sequel::MigrationNow that we have status table setup lets manage the user status restfully. Lets create a controller for REST enabled actions.
def up create_table :statuses do |u|
primary_key :id
integer :user_id
varchar :status, :size => 100
text :note
datetime :time_at
end
end
def down
execute "DROP TABLE statuses"
end
end
% merb-gen controller statusThis will create a new contoller now add four actions to allow for REST protocol.
class Statuses < ApplicationNow that we have this in controller, lets setup router.rb to use this controller restfully, Add thi s line to router.rb
# GET /statuses
# GET /statuses.xml
def index
@statuses = Status.filter('user_id = ?', session[:user_id])
end
# GET /statuses/1
# GET /statuses/1.xml
def show
@status = Status.filter('id = ?', params[:id])
end
# GET /statuses/new
def new
end
# GET /statuses/1;edit
def edit
end
# POST /statuses
# POST /statuses.xml
def create
Status.insert(:user_id => session[:user_id], :status => params["select_status"], :note => params["status_note"], :time_at => Time.now)
end
# PUT /statuses/1
# PUT /statuses/1.xml
def update
Status.filter('id = ?', params[:id]).update(:status => params["select_status"])
end
# DELETE /statuses/1
# DELETE /statuses/1.xml
def destroy
Status.filter(:id => params[:id]).delete
end
end
r.resources :statusesNow you can call the the statuses controller restfully from anywhere. Check out Ezra's post on Restful routes
No comments:
Post a Comment