Plantilla NetBeans para controladores REST
¿A que creyeron que estaba muerto? Pues no… Esto de tener un trabajo y aparte mantener un blog me parecia mucho más fácil antes de iniciarlo…
En fin, les paso una plantilla para NetBeans que sirve para generar una serie de acciones REST estándar. Tu solo necesitas introducir dos variables y el nombre del modelo.
La idea (y práctiamente todo el código) me la robe de “Softies on Rails”:http://www.softiesonrails.com/2007/12/14/textmate-snippet-for-restful-controller.
“Behold”:
[ruby]
def index
@${items} = ${Model}.find :all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @${items} }
end
end
def show
@${item} = ${Model}.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @${item} }
end
end
def new
@${item} = ${Model}.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @${item} }
end
end
def edit
@${item} = ${Model}.find(params[:id])
end
def create
@${item} = ${Model}.new(params[:${item}])
respond_to do |format|
if @${item}.save
flash[:notice] = ‘$2 was successfully created.’
format.html { redirect_to(@${item}) }
format.xml { render :xml => @${item}, :status => :created, :location => @${item} }
else
format.html { render :action => “new” }
format.xml { render :xml => @${item}.errors, :status => :unprocessable_entity }
end
end
end
def update
@${item} = ${Model}.find(params[:id])
respond_to do |format|
if @${item}.update_attributes(params[:${item}])
flash[:notice] = ‘${Model} was successfully updated.’
format.html { redirect_to(@${item}) }
format.xml { head :ok }
else
format.html { render :action => “edit” }
format.xml { render :xml => @${item}.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@${item} = ${Model}.find(params[:id])
@${item}.destroy
respond_to do |format|
format.html { redirect_to(admin_${items}_url) }
format.xml { head :ok }
end
end
[/ruby]
Por ahí le falta una arroba (@) en la acción “create”. Sí se la puse, pero ahi arriba no se porque no aparece. Consideralo un acertijo.