Permalink
Browse files

Use exception handling in Controller templates to handle Validations.…

… This is new default behaviour in Sequel 3.x where validation raise Sequel::ValidationFailed exception.
  • Loading branch information...
1 parent 665edf8 commit 5cf6175be5f446bc272f290156b4240a28d19d36 @pk committed Aug 22, 2009
Showing with 11 additions and 8 deletions.
  1. +11 −8 lib/generators/templates/resource_controller/app/controllers/%file_name%.rb
View
19 lib/generators/templates/resource_controller/app/controllers/%file_name%.rb
@@ -25,9 +25,10 @@ def new
# POST /<%= resource_path %>
def create
@<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
- if @<%= singular_model %>.save
+ begin
+ @<%= singular_model %>.save
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
- else
+ rescue Sequel::ValidationFailed
render :new
end
end
@@ -44,23 +45,25 @@ def edit
def update
@<%= singular_model %> = <%= model_class_name %>[params[:id]]
raise NotFound unless @<%= singular_model %>
- if @<%= singular_model %>.update(params[:<%= singular_model %>])
+ begin
+ @<%= singular_model %>.update(params[:<%= singular_model %>])
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
- else
- raise BadRequest
+ rescue Sequel::ValidationFailed
+ render :edit
end
end
# DELETE /<%= resource_path %>/:id
def destroy
@<%= singular_model %> = <%= model_class_name %>[params[:id]]
raise NotFound unless @<%= singular_model %>
- if @<%= singular_model %>.destroy
+ begin
+ @<%= singular_model %>.destroy
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>s)
- else
+ rescue Sequel::Error
raise BadRequest
end
end
end
-<% end -%>
+<% end -%>

0 comments on commit 5cf6175

Please sign in to comment.