Skip to content

Commit

Permalink
after_destroy フック
Browse files Browse the repository at this point in the history
destroy アクション内で例外を捕捉
  • Loading branch information
kyanny committed Jan 25, 2010
1 parent 3697273 commit 2dbee34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion depot/app/controllers/users_controller.rb
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
class UsersController < ApplicationController
# GET /users
# GET /users.xml
Expand Down Expand Up @@ -75,7 +76,12 @@ def update
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy
begin
flash[:notice] = "ユーザ #{@user.name} は削除されました"
@user.destroy
rescue Exception => e
flash[:notice] = e.message
end

respond_to do |format|
format.html { redirect_to(users_url) }
Expand Down
6 changes: 6 additions & 0 deletions depot/app/models/user.rb
Expand Up @@ -35,6 +35,12 @@ def self.authenticate (name, password)
user
end

def after_destroy
if User.count.zero?
raise "最後のユーザは削除できません"
end
end

private

def password_non_blank
Expand Down

0 comments on commit 2dbee34

Please sign in to comment.