RESTfulなひな形生成 - merb-gen resource_controller

$ merb-gen resource_controller user

     [ADDED]  spec/requests/user_spec.rb
     [ADDED]  app/controllers/user.rb
     [ADDED]  app/views/user/index.html.erb
     [ADDED]  app/views/user/show.html.erb
     [ADDED]  app/views/user/edit.html.erb
     [ADDED]  app/views/user/new.html.erb
     [ADDED]  app/helpers/user_helper.rb

viewのファイルとかがデフォルトで生成されるのはmerbのgeneratorの特徴だなぁ。
Controllerのひな形もresourceにあわせて生成される。これはいいですね。REST信者になれそうです。

class User < Application
  # provides :xml, :yaml, :js

  def index
    @user = User.all
    display @user
  end

  def show(id)
    @user = User.get(id)
    raise NotFound unless @user
    display @user
  end

  def new
    only_provides :html
    @user = User.new
    display @user
  end

  def edit(id)
    only_provides :html
    @user = User.get(id)
    raise NotFound unless @user
    display @user
  end

  def create(user)
    @user = User.new(user)
    if @user.save
      redirect resource(@user), :message => {:notice => "User was successfully created"}
    else
      message[:error] = "User failed to be created"
      render :new
    end
  end

  def update(id, user)
    @user = User.get(id)
    raise NotFound unless @user
    if @user.update_attributes(user)
       redirect resource(@user)
    else
      display @user, :edit
    end
  end

  def destroy(id)
    @user = User.get(id)
    raise NotFound unless @user
    if @user.destroy
      redirect resource(:user)
    else
      raise InternalServerError
    end
  end

end # User