Friday, November 2, 2007

Merb Crud Controller

The other day I decided to try a complete CRUD controller using the new provides format and Active Record. It supports XML, Javascript, YAML formats where appropriate and HTML across all actions. The built in error handling in Merb also helps to clean things up.

Well. Here it is, for better or worse. I really like it though ;)

class Posts < Application
provides :xml, :js, :yaml

def index
@posts = Post.find(:all)
render @posts
end

def show
@post = Post.find(params[:id])
render @post
end

def new
only_provides :html
@post = Post.new(params[:post])
render
end

def create
@post = Post.new(params[:post])
if @post.save
redirect url(:post, @post)
else
render :action => :new
end
end

def edit
only_provides :html
@post = Post.find(params[:id])
render
end

def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect url(:post, @post)
else
raise BadRequest
end
end

def destroy
@post = Post.find(params[:id])
if @post.destroy
redirect url(:posts)
else
raise BadRequest
end
end
end

2 comments:

Anonymous said...

Hi !.
You re, I guess , perhaps very interested to know how one can make real money .
There is no initial capital needed You may commense earning with as small sum of money as 20-100 dollars.

AimTrust is what you need
The firm incorporates an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

It is based in Panama with offices around the world.
Do you want to become a happy investor?
That`s your chance That`s what you wish in the long run!

I`m happy and lucky, I started to get real money with the help of this company,
and I invite you to do the same. It`s all about how to select a correct partner utilizes your funds in a right way - that`s it!.
I earn US$2,000 per day, and my first deposit was 1 grand only!
It`s easy to join , just click this link http://iqalomoc.o-f.com/uduleh.html
and go! Let`s take our chance together to feel the smell of real money

Anonymous said...

Hi !.
You may , perhaps curious to know how one can collect a huge starting capital .
There is no initial capital needed You may begin earning with as small sum of money as 20-100 dollars.

AimTrust is what you need
The company incorporates an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

It is based in Panama with offices everywhere: In USA, Canada, Cyprus.
Do you want to become an affluent person?
That`s your choice That`s what you wish in the long run!

I feel good, I started to get real money with the help of this company,
and I invite you to do the same. It`s all about how to select a proper partner utilizes your funds in a right way - that`s it!.
I take now up to 2G every day, and what I started with was a funny sum of 500 bucks!
It`s easy to join , just click this link http://ybigisokyt.lookseekpages.com/izawyqi.html
and lucky you`re! Let`s take our chance together to become rich