REST : scaffold_resource generated code

Hi, I have two questions about the code generated by the
scaffold_resource generator (I think about this code as a general hints,
good habits, even if most of the time I think that generators are
weirds)

  • DRY : in a real application the code (the “business logic”) in the
    create and update methods is quite the same - In previous projects I
    always used to have a single method handling the two based on the case
    it was a POST or a GET request - Which solution is the best ?

  • respond_to do |format| : why does the generator produces both code for
    .html and .xml formats ? What am I supposed to do with the .xml part if
    I not working on a web service or such ? Remove it ?

Thanks

Unmodified code :

class ClientsController < ApplicationController

GET /clients

GET /clients.xml

def index
@clients = Client.find(:all)

respond_to do |format|
  format.html # index.rhtml
  format.xml  { render :xml => @clients.to_xml }
end

end

GET /clients/1

GET /clients/1.xml

def show
@client = Client.find(params[:id])

respond_to do |format|
  format.html # show.rhtml
  format.xml  { render :xml => @client.to_xml }
end

end

GET /clients/new

def new
@client = Client.new
end

GET /clients/1;edit

def edit
@client = Client.find(params[:id])
end

POST /clients

POST /clients.xml

def create
@client = Client.new(params[:client])

respond_to do |format|
  if @client.save
    flash[:notice] = 'Client was successfully created.'
    format.html { redirect_to client_url(@client) }
    format.xml  { head :created, :location => client_url(@client) }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @client.errors.to_xml }
  end
end

end

PUT /clients/1

PUT /clients/1.xml

def update
@client = Client.find(params[:id])

respond_to do |format|
  if @client.update_attributes(params[:client])
    flash[:notice] = 'Client was successfully updated.'
    format.html { redirect_to client_url(@client) }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @client.errors.to_xml }
  end
end

end

DELETE /clients/1

DELETE /clients/1.xml

def destroy
@client = Client.find(params[:id])
@client.destroy

respond_to do |format|
  format.html { redirect_to clients_url }
  format.xml  { head :ok }
end

end
end

  • DRY : in a real application the code (the “business logic”) in the
    create and update methods is quite the same - In previous projects I
    always used to have a single method handling the two based on the case
    it was a POST or a GET request - Which solution is the best ?

  • respond_to do |format| : why does the generator produces both code for
    .html and .xml formats ? What am I supposed to do with the .xml part if
    I not working on a web service or such ? Remove it ?

Create a private method and put the common code that you can share for
create and update.

If you don’t need xml, you can remove it. The purpose of scaffold is
to get you started with the CRUD way of doing things.